<!DOCTYPE html><!--[if IEMobile 7 ]><html class="no-js iem7" lang="zh-CN"><![endif]--><!--[if lt IE 9]><html class="no-js lte-ie8" lang="zh-CN"><![endif]--><!--[if (gt IE 8)|(gt IEMobile 7)|!(IEMobile)|!(IE)]><!--><html class="no-js" lang="zh-CN"><!--<![endif]--><head><meta charset="UTF-8"><title>js 中常见问题整理 400+</title><meta name="author" content="BARAN"><link rel="shortcut icon" href="/favicon.ico"><meta name="HandheldFriendly" content="True"><meta name="MobileOptimized" content="320"><meta name="viewport" content="width=device-width,minimum-scale=1,maximum-scale=1"><link rel="canonical" href="https://cxvh.gitee.io/2020/12/03/web/js/problem/"><link rel="alternate" href="/atom.xml" type="application/atom+xml"><script src="/js/modernizr-2.0.js"></script><script src="/js/jquery-2.2.0.min.js"></script><script src="https://cdn.cxvh.com/script/js/typed.min.js"></script><script>window.twttr=function(t,e,r){var n=t.getElementsByTagName(e)[0],i=window.twttr||{};return t.getElementById(r)||((e=t.createElement(e)).id=r,e.src="https://platform.twitter.com/widgets.js",n.parentNode.insertBefore(e,n),i._e=[],i.ready=function(t){i._e.push(t)}),i}(document,"script","twitter-wjs")</script><link rel="me" href="https://twitter.com/your_twitter"><link rel="stylesheet" href="/css/octo.css"><link href="https://fonts.googleapis.com/css?family=PT+Serif:regular,italic,bold,bolditalic" rel="stylesheet" type="text/css"><link href="https://fonts.googleapis.com/css?family=PT+Sans:regular,italic,bold,bolditalic" rel="stylesheet" type="text/css"><script>var _hmt=_hmt||[];!function(){var e=document.createElement("script");e.src="https://hm.baidu.com/hm.js?5b59e0166baa8ced0505ce9e1a9f4890";var t=document.getElementsByTagName("script")[0];t.parentNode.insertBefore(e,t)}()</script><script async src="https://www.googletagmanager.com/gtag/js?id=G-YBTMS716H5"></script><script>function gtag(){dataLayer.push(arguments)}window.dataLayer=window.dataLayer||[],gtag("js",new Date),gtag("config","G-YBTMS716H5")</script><base target="_black"><meta name="generator" content="Hexo 5.3.0"></head><body><header role="banner"><hgroup><h1><a href="/" data-text="BARAN 的笔记 🔥">BARAN 的笔记 🔥</a></h1><h2 title="Eternity is not adistance but adecision."></h2></hgroup></header><nav role="navigation"><ul class="subscription" data-subscription="rss"><li><a href="/atom.xml" rel="subscribe-rss" title="subscribe via RSS">RSS</a></li></ul><form action="https://google.com/search" method="get"><fieldset role="search"><input type="hidden" name="q" value="site:https://cxvh.gitee.io/"> <input class="search" type="text" name="q" results="0" placeholder="Search"></fieldset></form><ul class="main-navigation"><li><a href="/">首页</a></li><li><a href="/archives">归档</a></li><li><a href="/tags">标签</a></li><li><a href="/categories">分类</a></li></ul></nav><div id="main"><div id="content"><div><article class="hentry" role="article"><header><h1 class="entry-title">js 中常见问题整理 400+</h1><p class="meta"><time datetime="2020-12-02T16:00:00.000Z" data-updated="true">Thu Dec 03 2020</time></p></header><div class="entry-content"><p>有时间了翻译. 😂😂😂</p><h3 id="目录"><a href="#目录" class="headerlink" title="目录"></a>目录</h3><table><thead><tr><th>序号</th><th>问题</th></tr></thead><tbody><tr><td>1</td><td><a href="#JS-%E5%88%9B%E5%BB%BA%E5%AF%B9%E8%B1%A1%E6%9C%89%E9%82%A3%E4%BA%9B%E6%96%B9%E6%B3%95">JS 创建对象有那些方法</a></td></tr><tr><td>2</td><td><a href="#%E4%BB%80%E4%B9%88%E6%98%AF%E5%8E%9F%E5%9E%8B%E9%93%BE">什么是原型链</a></td></tr><tr><td>3</td><td><a href="#what-is-the-difference-between-call-apply-and-bind">What is the difference between Call, Apply and Bind</a></td></tr><tr><td>4</td><td><a href="#what-is-json-and-its-common-operations">What is JSON and its common operations</a></td></tr><tr><td>5</td><td><a href="#what-is-the-purpose-of-array-slice-method">What is the purpose of array slice method</a></td></tr><tr><td>6</td><td><a href="#what-is-the-purpose-of-array-splice-method">What is the purpose of array splice method</a></td></tr><tr><td>7</td><td><a href="#what-is-the-difference-between-slice-and-splice">What is the difference between slice and splice</a></td></tr><tr><td>8</td><td><a href="#how-do-you-compare-object-and-map">How do you compare Object and Map</a></td></tr><tr><td>9</td><td><a href="#what-is-the-difference-between-==-and-===-operators">What is the difference between == and === operators</a></td></tr><tr><td>10</td><td><a href="#what-are-lambda-or-arrow-functions">What are lambda or arrow functions</a></td></tr><tr><td>11</td><td><a href="#what-is-a-first-class-function">What is a first class function</a></td></tr><tr><td>12</td><td><a href="#what-is-a-first-order-function">What is a first order function</a></td></tr><tr><td>13</td><td><a href="#what-is-a-higher-order-function">What is a higher order function</a></td></tr><tr><td>14</td><td><a href="#what-is-a-unary-function">What is a unary function</a></td></tr><tr><td>15</td><td><a href="#what-is-currying-function">What is currying function</a></td></tr><tr><td>16</td><td><a href="#what-is-a-pure-function">What is a pure function</a></td></tr><tr><td>17</td><td><a href="#what-is-the-purpose-of-let-keyword">What is the purpose of let keyword</a></td></tr><tr><td>18</td><td><a href="#what-is-the-difference-between-let-and-var">What is the difference between let and var</a></td></tr><tr><td>19</td><td><a href="#what-is-the-reason-to-choose-the-name-let-as-keyword">What is the reason to choose the name let as keyword</a></td></tr><tr><td>20</td><td><a href="#how-do-you-redeclare-variables-in-switch-block-without-an-error">How do you redeclare variables in switch block without an error</a></td></tr><tr><td>21</td><td><a href="#what-is-temporal-dead-zone">What is Temporal Dead Zone</a></td></tr><tr><td>22</td><td><a href="#what-is-iife-(-immediately-invoked-function-expression-)-">What is IIFE(Immediately Invoked Function Expression)</a></td></tr><tr><td>23</td><td><a href="#what-is-the-benefit-of-using-modules">What is the benefit of using modules</a></td></tr><tr><td>24</td><td><a href="#what-is-memoization">What is memoization</a></td></tr><tr><td>25</td><td><a href="#what-is-hoisting">What is Hoisting</a></td></tr><tr><td>26</td><td><a href="#what-are-classes-in-es6">What are classes in ES6</a></td></tr><tr><td>27</td><td><a href="#what-are-closures">What are closures</a></td></tr><tr><td>28</td><td><a href="#what-are-modules">What are modules</a></td></tr><tr><td>29</td><td><a href="#why-do-you-need-modules">Why do you need modules</a></td></tr><tr><td>30</td><td><a href="#what-is-scope-in-javascript">What is scope in javascript</a></td></tr><tr><td>31</td><td><a href="#what-is-a-service-worker">What is a service worker</a></td></tr><tr><td>32</td><td><a href="#how-do-you-manipulate-dom-using-service-worker">How do you manipulate DOM using service worker</a></td></tr><tr><td>33</td><td><a href="#how-do-you-reuse-information-across-service-worker-restarts">How do you reuse information across service worker restarts</a></td></tr><tr><td>34</td><td><a href="#what-is-indexeddb">What is IndexedDB</a></td></tr><tr><td>35</td><td><a href="#what-is-web-storage">What is web storage</a></td></tr><tr><td>36</td><td><a href="#what-is-a-post-message">What is a post message</a></td></tr><tr><td>37</td><td><a href="#what-is-a-cookie">What is a cookie</a></td></tr><tr><td>38</td><td><a href="#why-do-you-need-a-cookie">Why do you need a Cookie</a></td></tr><tr><td>39</td><td><a href="#what-are-the-options-in-a-cookie">What are the options in a cookie</a></td></tr><tr><td>40</td><td><a href="#how-do-you-delete-a-cookie">How do you delete a cookie</a></td></tr><tr><td>41</td><td><a href="#What-are-the-differences-between-cookie,-local-storage-and-session-storage">What are the differences between cookie, local storage and session storage</a></td></tr><tr><td>42</td><td><a href="#what-is-the-main-difference-between-localstorage-and-sessionstorage">What is the main difference between localStorage and sessionStorage</a></td></tr><tr><td>43</td><td><a href="#how-do-you-access-web-storage">How do you access web storage</a></td></tr><tr><td>44</td><td><a href="#what-are-the-methods-available-on-session-storage">What are the methods available on session storage</a></td></tr><tr><td>45</td><td><a href="#what-is-a-storage-event-and-its-event-handler">What is a storage event and its event handler</a></td></tr><tr><td>46</td><td><a href="#why-do-you-need-web-storage">Why do you need web storage</a></td></tr><tr><td>47</td><td><a href="#how-do-you-check-web-storage-browser-support">How do you check web storage browser support</a></td></tr><tr><td>48</td><td><a href="#how-do-you-check-web-workers-browser-support">How do you check web workers browser support</a></td></tr><tr><td>49</td><td><a href="#give-an-example-of-web-worker">Give an example of web worker</a></td></tr><tr><td>50</td><td><a href="#what-are-the-restrictions-of-web-workers-on-dom">What are the restrictions of web workers on DOM</a></td></tr><tr><td>51</td><td><a href="#what-is-a-promise">What is a promise</a></td></tr><tr><td>52</td><td><a href="#why-do-you-need-a-promise">Why do you need a promise</a></td></tr><tr><td>53</td><td><a href="#what-are-the-three-states-of-promise">What are the three states of promise</a></td></tr><tr><td>54</td><td><a href="#what-is-a-callback-function">What is a callback function</a></td></tr><tr><td>55</td><td><a href="#why-do-we-need-callbacks">Why do we need callbacks</a></td></tr><tr><td>56</td><td><a href="#what-is-a-callback-hell">What is a callback hell</a></td></tr><tr><td>57</td><td><a href="#what-is-server-sent-events">What is server-sent events</a></td></tr><tr><td>58</td><td><a href="#how-do-you-receive-server-sent-event-notifications">How do you receive server-sent event notifications</a></td></tr><tr><td>59</td><td><a href="#how-do-you-check-browser-support-for-server-sent-events">How do you check browser support for server-sent events</a></td></tr><tr><td>60</td><td><a href="#what-are-the-events-available-for-server-sent-events">What are the events available for server sent events</a></td></tr><tr><td>61</td><td><a href="#what-are-the-main-rules-of-promise">What are the main rules of promise</a></td></tr><tr><td>62</td><td><a href="#what-is-callback-in-callback">What is callback in callback</a></td></tr><tr><td>63</td><td><a href="#what-is-promise-chaining">What is promise chaining</a></td></tr><tr><td>64</td><td><a href="#what-is-promise.all">What is promise.all</a></td></tr><tr><td>65</td><td><a href="#what-is-the-purpose-of-race-method-in-promise">What is the purpose of race method in promise</a></td></tr><tr><td>66</td><td><a href="#what-is-a-strict-mode-in-javascript">What is a strict mode in javascript</a></td></tr><tr><td>67</td><td><a href="#why-do-you-need-strict-mode">Why do you need strict mode</a></td></tr><tr><td>68</td><td><a href="#how-do-you-declare-strict-mode">How do you declare strict mode</a></td></tr><tr><td>69</td><td><a href="#what-is-the-purpose-of-double-exclamation">What is the purpose of double exclamation</a></td></tr><tr><td>70</td><td><a href="#what-is-the-purpose-of-delete-operator">What is the purpose of delete operator</a></td></tr><tr><td>71</td><td><a href="#what-is-typeof-operator">What is typeof operator</a></td></tr><tr><td>72</td><td><a href="#what-is-undefined-property">What is undefined property</a></td></tr><tr><td>73</td><td><a href="#what-is-null-value">What is null value</a></td></tr><tr><td>74</td><td><a href="#what-is-the-difference-between-null-and-undefined">What is the difference between null and undefined</a></td></tr><tr><td>75</td><td><a href="#What-is-eval">What is eval</a></td></tr><tr><td>76</td><td><a href="#what-is-the-difference-between-window-and-document">What is the difference between window and document</a></td></tr><tr><td>77</td><td><a href="#how-do-you-access-history-in-javascript">How do you access history in javascript</a></td></tr><tr><td>78</td><td><a href="#what-are-the-javascript-data-types">What are the javascript data types</a></td></tr><tr><td>79</td><td><a href="#what-is-isnan">What is isNaN</a></td></tr><tr><td>80</td><td><a href="#what-are-the-differences-between-undeclared-and-undefined-variables">What are the differences between undeclared and undefined variables</a></td></tr><tr><td>81</td><td><a href="#what-are-global-variables">What are global variables</a></td></tr><tr><td>82</td><td><a href="#what-are-the-problems-with-global-variables">What are the problems with global variables</a></td></tr><tr><td>83</td><td><a href="#what-is-nan-property">What is NaN property</a></td></tr><tr><td>84</td><td><a href="#what-is-the-purpose-of-isfinite-function">What is the purpose of isFinite function</a></td></tr><tr><td>85</td><td><a href="#what-is-an-event-flow">What is an event flow</a></td></tr><tr><td>86</td><td><a href="#what-is-event-bubbling">What is event bubbling</a></td></tr><tr><td>87</td><td><a href="#what-is-event-capturing">What is event capturing</a></td></tr><tr><td>88</td><td><a href="#how-do-you-submit-a-form-using-javascript">How do you submit a form using JavaScript</a></td></tr><tr><td>89</td><td><a href="#how-do-you-find-operating-system-details">How do you find operating system details</a></td></tr><tr><td>90</td><td><a href="#what-is-the-difference-between-document-load-and-domcontentloaded-events">What is the difference between document load and DOMContentLoaded events</a></td></tr><tr><td>91</td><td><a href="#what-is-the-difference-between-native,-host-and-user-objects">What is the difference between native, host and user objects</a></td></tr><tr><td>92</td><td><a href="#what-are-the-tools-or-techniques-used-for-debugging-javascript-code">What are the tools or techniques used for debugging JavaScript code</a></td></tr><tr><td>93</td><td><a href="#what-are-the-pros-and-cons-of-promises-over-callbacks">What are the pros and cons of promises over callbacks</a></td></tr><tr><td>94</td><td><a href="#what-is-the-difference-between-an-attribute-and-a-property">What is the difference between an attribute and a property</a></td></tr><tr><td>95</td><td><a href="#what-is-same-origin-policy">What is same-origin policy</a></td></tr><tr><td>96</td><td><a href="#what-is-the-purpose-of-void-0">What is the purpose of void 0</a></td></tr><tr><td>97</td><td><a href="#is-javascript-a-compiled-or-interpreted-language">Is JavaScript a compiled or interpreted language</a></td></tr><tr><td>98</td><td><a href="#is-javascript-a-case-sensitive-language">Is JavaScript a case-sensitive language</a></td></tr><tr><td>99</td><td><a href="#is-there-any-relation-between-java-and-javascript">Is there any relation between Java and JavaScript</a></td></tr><tr><td>100</td><td><a href="#what-are-events">What are events</a></td></tr><tr><td>101</td><td><a href="#who-created-javascript">Who created javascript</a></td></tr><tr><td>102</td><td><a href="#what-is-the-use-of-preventdefault-method">What is the use of preventDefault method</a></td></tr><tr><td>103</td><td><a href="#what-is-the-use-of-stoppropagation-method">What is the use of stopPropagation method</a></td></tr><tr><td>104</td><td><a href="#what-are-the-steps-involved-in-return-false">What are the steps involved in return false</a></td></tr><tr><td>105</td><td><a href="#what-is-bom">What is BOM</a></td></tr><tr><td>106</td><td><a href="#what-is-the-use-of-settimeout">What is the use of setTimeout</a></td></tr><tr><td>107</td><td><a href="#what-is-the-use-of-setinterval">What is the use of setInterval</a></td></tr><tr><td>108</td><td><a href="#why-is-javascript-treated-as-single-threaded">Why is JavaScript treated as Single threaded</a></td></tr><tr><td>109</td><td><a href="#what-is-an-event-delegation">What is an event delegation</a></td></tr><tr><td>110</td><td><a href="#what-is-ecmascript">What is ECMAScript</a></td></tr><tr><td>111</td><td><a href="#what-is-json">What is JSON</a></td></tr><tr><td>112</td><td><a href="#what-are-the-syntax-rules-of-json">What are the syntax rules of JSON</a></td></tr><tr><td>113</td><td><a href="#what-is-the-purpose-json-stringify">What is the purpose JSON stringify</a></td></tr><tr><td>114</td><td><a href="#how-do-you-parse-json-string">How do you parse JSON string</a></td></tr><tr><td>115</td><td><a href="#why-do-you-need-json">Why do you need JSON</a></td></tr><tr><td>116</td><td><a href="#what-are-pwas?">What are PWAs</a></td></tr><tr><td>117</td><td><a href="#what-is-the-purpose-of-cleartimeout-method">What is the purpose of clearTimeout method</a></td></tr><tr><td>118</td><td><a href="#what-is-the-purpose-of-clearinterval-method">What is the purpose of clearInterval method</a></td></tr><tr><td>119</td><td><a href="#how-do-you-redirect-new-page-in-javascript">How do you redirect new page in javascript</a></td></tr><tr><td>120</td><td><a href="#how-do-you-check-whether-a-string-contains-a-substring">How do you check whether a string contains a substring</a></td></tr><tr><td>121</td><td><a href="#how-do-you-validate-an-email-in-javascript">How do you validate an email in javascript</a></td></tr><tr><td>122</td><td><a href="#how-do-you-get-the-current-url-with-javascript">How do you get the current url with javascript</a></td></tr><tr><td>123</td><td><a href="#what-are-the-various-url-properties-of-location-object">What are the various url properties of location object</a></td></tr><tr><td>124</td><td><a href="#how-do-get-query-string-values-in-javascript">How do get query string values in javascript</a></td></tr><tr><td>125</td><td><a href="#how-do-you-check-if-a-key-exists-in-an-object">How do you check if a key exists in an object</a></td></tr><tr><td>126</td><td><a href="#how-do-you-loop-through-or-enumerate-javascript-object">How do you loop through or enumerate javascript object</a></td></tr><tr><td>127</td><td><a href="#how-do-you-test-for-an-empty-object">How do you test for an empty object</a></td></tr><tr><td>128</td><td><a href="#what-is-an-arguments-object">What is an arguments object</a></td></tr><tr><td>129</td><td><a href="#how-do-you-make-first-letter-of-the-string-in-an-uppercase">How do you make first letter of the string in an uppercase</a></td></tr><tr><td>130</td><td><a href="#what-are-the-pros-and-cons-of-for-loop">What are the pros and cons of for loop</a></td></tr><tr><td>131</td><td><a href="#how-do-you-display-the-current-date-in-javascript">How do you display the current date in javascript</a></td></tr><tr><td>132</td><td><a href="#how-do-you-compare-two-date-objects">How do you compare two date objects</a></td></tr><tr><td>133</td><td><a href="#how-do-you-check-if-a-string-starts-with-another-string">How do you check if a string starts with another string</a></td></tr><tr><td>134</td><td><a href="#how-do-you-trim-a-string-in-javascript">How do you trim a string in javascript</a></td></tr><tr><td>135</td><td><a href="#how-do-you-add-a-key-value-pair-in-javascript">How do you add a key value pair in javascript</a></td></tr><tr><td>136</td><td><a href="#is-the-'!--'-notation-represents-a-special-operator">Is the ‘!–’ notation represents a special operator</a></td></tr><tr><td>137</td><td><a href="#how-do-you-assign-default-values-to-variables">How do you assign default values to variables</a></td></tr><tr><td>138</td><td><a href="#how-do-you-define-multiline-strings">How do you define multiline strings</a></td></tr><tr><td>139</td><td><a href="#what-is-an-app-shell-model">What is an app shell model</a></td></tr><tr><td>140</td><td><a href="#can-we-define-properties-for-functions">Can we define properties for functions</a></td></tr><tr><td>141</td><td><a href="#what-is-the-way-to-find-the-number-of-parameters-expected-by-a-function">What is the way to find the number of parameters expected by a function</a></td></tr><tr><td>142</td><td><a href="#what-is-a-polyfill">What is a polyfill</a></td></tr><tr><td>143</td><td><a href="#what-are-break-and-continue-statements">What are break and continue statements</a></td></tr><tr><td>144</td><td><a href="#what-are-js-labels">What are js labels</a></td></tr><tr><td>145</td><td><a href="#what-are-the-benefits-of-keeping-declarations-at-the-top">What are the benefits of keeping declarations at the top</a></td></tr><tr><td>146</td><td><a href="#what-are-the-benefits-of-initializing-variables">What are the benefits of initializing variables</a></td></tr><tr><td>147</td><td><a href="#what-are-the-recommendations-to-create-new-object">What are the recommendations to create new object</a></td></tr><tr><td>148</td><td><a href="#how-do-you-define-json-arrays">How do you define JSON arrays</a></td></tr><tr><td>149</td><td><a href="#how-do-you-generate-random-integers">How do you generate random integers</a></td></tr><tr><td>150</td><td><a href="#can-you-write-a-random-integers-function-to-print-integers-with-in-a-range">Can you write a random integers function to print integers with in a range</a></td></tr><tr><td>151</td><td><a href="#what-is-tree-shaking">What is tree shaking</a></td></tr><tr><td>152</td><td><a href="#what-is-the-need-of-tree-shaking">What is the need of tree shaking</a></td></tr><tr><td>153</td><td><a href="#is-it-recommended-to-use-eval">Is it recommended to use eval</a></td></tr><tr><td>154</td><td><a href="#what-is-a-regular-expression">What is a Regular Expression</a></td></tr><tr><td>155</td><td><a href="#what-are-the-string-methods-available-in-regular-expression">What are the string methods available in Regular expression</a></td></tr><tr><td>156</td><td><a href="#what-are-modifiers-in-regular-expression">What are modifiers in regular expression</a></td></tr><tr><td>157</td><td><a href="#what-are-regular-expression-patterns">What are regular expression patterns</a></td></tr><tr><td>158</td><td><a href="#what-is-a-regexp-object">What is a RegExp object</a></td></tr><tr><td>159</td><td><a href="#how-do-you-search-a-string-for-a-pattern">How do you search a string for a pattern</a></td></tr><tr><td>160</td><td><a href="#what-is-the-purpose-of-exec-method">What is the purpose of exec method</a></td></tr><tr><td>161</td><td><a href="#how-do-you-change-style-of-a-html-element">How do you change style of a HTML element</a></td></tr><tr><td>162</td><td><a href="#what-would-be-the-result-of-1+2+'3'">What would be the result of 1+2+’3’</a></td></tr><tr><td>163</td><td><a href="#what-is-a-debugger-statement">What is a debugger statement</a></td></tr><tr><td>164</td><td><a href="#what-is-the-purpose-of-breakpoints-indebugging">What is the purpose of breakpoints in debugging</a></td></tr><tr><td>165</td><td><a href="#can-i-use-reserved-words-as-identifiers">Can I use reserved words as identifiers</a></td></tr><tr><td>166</td><td><a href="#how-do-you-detect-a-mobile-browser">How do you detect a mobile browser</a></td></tr><tr><td>167</td><td><a href="#how-do-you-detect-a-mobile-browser-without-regexp">How do you detect a mobile browser without regexp</a></td></tr><tr><td>168</td><td><a href="#how-do-you-get-the-image-width-and-height-using-js">How do you get the image width and height using JS</a></td></tr><tr><td>169</td><td><a href="#how-do-you-make-synchronous-http-request">How do you make synchronous HTTP request</a></td></tr><tr><td>170</td><td><a href="#how-do-you-make-asynchronous-http-request">How do you make asynchronous HTTP request</a></td></tr><tr><td>171</td><td><a href="#how-do-you-convert-date-to-another-timezone-in-javascript">How do you convert date to another timezone in javascript</a></td></tr><tr><td>172</td><td><a href="#what-are-the-properties-used-to-get-size-of-window">What are the properties used to get size of window</a></td></tr><tr><td>173</td><td><a href="#what-is-a-conditional-operator-in-javascript">What is a conditional operator in javascript</a></td></tr><tr><td>174</td><td><a href="#Can-you-apply-chaining-on-conditional-operator">Can you apply chaining on conditional operator</a></td></tr><tr><td>175</td><td><a href="#what-are-the-ways-to-execute-javascript-after-page-load">What are the ways to execute javascript after page load</a></td></tr><tr><td>176</td><td><a href="#what-is-the-difference-between-proto-and-prototype">What is the difference between proto and prototype</a></td></tr><tr><td>177</td><td><a href="#give-an-example-where-do-you-really-need-semicolon">Give an example where do you really need semicolon</a></td></tr><tr><td>178</td><td><a href="#what-is-a-freeze-method">What is a freeze method</a></td></tr><tr><td>179</td><td><a href="#what-is-the-purpose-of-freeze-method">What is the purpose of freeze method</a></td></tr><tr><td>180</td><td><a href="#why-do-i-need-to-use-freeze-method">Why do I need to use freeze method</a></td></tr><tr><td>181</td><td><a href="#how-do-you-detect-a-browser-language-preference">How do you detect a browser language preference</a></td></tr><tr><td>182</td><td><a href="#how-to-convert-string-to-title-case-with-javascript">How to convert string to title case with javascript</a></td></tr><tr><td>183</td><td><a href="#how-do-you-detect-javascript-disabled-in-the-page">How do you detect javascript disabled in the page</a></td></tr><tr><td>184</td><td><a href="#what-are-various-operators-supported-by-javascript">What are various operators supported by javascript</a></td></tr><tr><td>185</td><td><a href="#what-is-a-rest-parameter">What is a rest parameter</a></td></tr><tr><td>186</td><td><a href="#what-happens-if-you-do-not-use-rest-parameter-as-a-last-argument">What happens if you do not use rest parameter as a last argument</a></td></tr><tr><td>187</td><td><a href="#what-are-the-bitwise-operators-available-in-javascript">What are the bitwise operators available in javascript</a></td></tr><tr><td>188</td><td><a href="#what-is-a-spread-operator">What is a spread operator</a></td></tr><tr><td>189</td><td><a href="#how-do-you-determine-whether-object-is-frozen-or-not">How do you determine whether object is frozen or not</a></td></tr><tr><td>190</td><td><a href="#how-do-you-determine-two-values-same-or-not-using-object">How do you determine two values same or not using object</a></td></tr><tr><td>191</td><td><a href="#what-is-the-purpose-of-using-object-is-method">What is the purpose of using object is method</a></td></tr><tr><td>192</td><td><a href="#how-do-you-copy-properties-from-one-object-to-other">How do you copy properties from one object to other</a></td></tr><tr><td>193</td><td><a href="#what-are-the-applications-of-assign-method">What are the applications of assign method</a></td></tr><tr><td>194</td><td><a href="#what-is-a-proxy-object">What is a proxy object</a></td></tr><tr><td>195</td><td><a href="#what-is-the-purpose-of-seal-method">What is the purpose of seal method</a></td></tr><tr><td>196</td><td><a href="#what-are-the-applications-of-seal-method">What are the applications of seal method</a></td></tr><tr><td>197</td><td><a href="#what-are-the-differences-between-freeze-and-seal-methods">What are the differences between freeze and seal methods</a></td></tr><tr><td>198</td><td><a href="#how-do-you-determine-if-an-object-is-sealed-or-not">How do you determine if an object is sealed or not</a></td></tr><tr><td>199</td><td><a href="#how-do-you-get-enumerable-key-and-value-pairs">How do you get enumerable key and value pairs</a></td></tr><tr><td>200</td><td><a href="#what-is-the-main-difference-between-object.values-and-object.entries-method">What is the main difference between Object.values and Object.entries method</a></td></tr><tr><td>201</td><td><a href="#how-can-you-get-the-list-of-keys-of-any-object">How can you get the list of keys of any object</a></td></tr><tr><td>202</td><td><a href="#how-do-you-create-an-object-with-prototype">How do you create an object with prototype</a></td></tr><tr><td>203</td><td><a href="#what-is-a-weakset">What is a WeakSet</a></td></tr><tr><td>204</td><td><a href="#what-are-the-differences-between-weakset-and-set">What are the differences between WeakSet and Set</a></td></tr><tr><td>205</td><td><a href="#list-down-the-collection-of-methods-available-on-weakset">List down the collection of methods available on WeakSet</a></td></tr><tr><td>206</td><td><a href="#what-is-a-weakmap">What is a WeakMap</a></td></tr><tr><td>207</td><td><a href="#what-are-the-differences-between-weakmap-and-map">What are the differences between WeakMap and Map</a></td></tr><tr><td>208</td><td><a href="#list-down-the-collection-of-methods-available-on-weakmap">List down the collection of methods available on WeakMap</a></td></tr><tr><td>209</td><td><a href="#what-is-the-purpose-of-uneval">What is the purpose of uneval</a></td></tr><tr><td>210</td><td><a href="#how-do-you-encode-an-url">How do you encode an URL</a></td></tr><tr><td>211</td><td><a href="#how-do-you-decode-an-url">How do you decode an URL</a></td></tr><tr><td>212</td><td><a href="#how-do-you-print-the-contents-of-web-page">How do you print the contents of web page</a></td></tr><tr><td>213</td><td><a href="#what-is-the-difference-between-uneval-and-eval">What is the difference between uneval and eval</a></td></tr><tr><td>214</td><td><a href="#what-is-an-anonymous-function">What is an anonymous function</a></td></tr><tr><td>215</td><td><a href="#what-is-the-precedence-order-between-local-and-global-variables">What is the precedence order between local and global variables</a></td></tr><tr><td>216</td><td><a href="#what-are-javascript-accessors">What are javascript accessors</a></td></tr><tr><td>217</td><td><a href="#how-do-you-define-property-on-object-constructor">How do you define property on Object constructor</a></td></tr><tr><td>218</td><td><a href="#what-is-the-difference-between-get-and-defineproperty">What is the difference between get and defineProperty</a></td></tr><tr><td>219</td><td><a href="#what-are-the-advantages-of-getters-and-setters">What are the advantages of Getters and Setters</a></td></tr><tr><td>220</td><td><a href="#can-i-add-getters-and-setters-using-defineproperty-method">Can I add getters and setters using defineProperty method</a></td></tr><tr><td>221</td><td><a href="#what-is-the-purpose-of-switch-case">What is the purpose of switch-case</a></td></tr><tr><td>222</td><td><a href="#what-are-the-conventions-to-be-followed-for-the-usage-of-swtich-case">What are the conventions to be followed for the usage of swtich case</a></td></tr><tr><td>223</td><td><a href="#what-are-primitive-data-types">What are primitive data types</a></td></tr><tr><td>224</td><td><a href="#what-are-the-different-ways-to-access-object-properties">What are the different ways to access object properties</a></td></tr><tr><td>225</td><td><a href="#what-are-the-function-parameter-rules">What are the function parameter rules</a></td></tr><tr><td>226</td><td><a href="#what-is-an-error-object">What is an error object</a></td></tr><tr><td>227</td><td><a href="#when-you-get-a-syntax-error">When you get a syntax error</a></td></tr><tr><td>228</td><td><a href="#what-are-the-different-error-names-from-error-object">What are the different error names from error object</a></td></tr><tr><td>229</td><td><a href="#what-are-the-various-statements-in-error-handling">What are the various statements in error handling</a></td></tr><tr><td>230</td><td><a href="#what-are-the-two-types-of-loops-in-javascript">What are the two types of loops in javascript</a></td></tr><tr><td>231</td><td><a href="#what-is-nodejs">What is nodejs</a></td></tr><tr><td>232</td><td><a href="#what-is-an-intl-object">What is an Intl object</a></td></tr><tr><td>233</td><td><a href="#how-do-you-perform-language-specific-date-and-time-formatting">How do you perform language specific date and time formatting</a></td></tr><tr><td>234</td><td><a href="#what-is-an-iterator">What is an Iterator</a></td></tr><tr><td>235</td><td><a href="#how-does-synchronous-iteration-works">How does synchronous iteration works</a></td></tr><tr><td>236</td><td><a href="#what-is-an-event-loop">What is an event loop</a></td></tr><tr><td>237</td><td><a href="#what-is-call-stack">What is call stack</a></td></tr><tr><td>238</td><td><a href="#what-is-an-event-queue">What is an event queue</a></td></tr><tr><td>239</td><td><a href="#what-is-a-decorator">What is a decorator</a></td></tr><tr><td>240</td><td><a href="#what-are-the-properties-of-intl-object">What are the properties of Intl object</a></td></tr><tr><td>241</td><td><a href="#what-is-an-unary-operator">What is an Unary operator</a></td></tr><tr><td>242</td><td><a href="#how-do-you-sort-elements-in-an-array">How do you sort elements in an array</a></td></tr><tr><td>243</td><td><a href="#what-is-the-purpose-of-comparefunction-while-sorting-arrays">What is the purpose of compareFunction while sorting arrays</a></td></tr><tr><td>244</td><td><a href="#how-do-you-reversing-an-array">How do you reversing an array</a></td></tr><tr><td>245</td><td><a href="#how-do-you-find-min-and-max-value-in-an-array">How do you find min and max value in an array</a></td></tr><tr><td>246</td><td><a href="#how-do-you-find-min-and-max-values-without--math-functions">How do you find min and max values without Math functions</a></td></tr><tr><td>247</td><td><a href="#what-is-an-empty-statement-and-purpose-of-it">What is an empty statement and purpose of it</a></td></tr><tr><td>248</td><td><a href="#how-do-you-get-meta-data-of-a-module">How do you get meta data of a module</a></td></tr><tr><td>249</td><td><a href="#what-is-a-comma-operator">What is a comma operator</a></td></tr><tr><td>250</td><td><a href="#what-is-the-advantage-of-a-comma-operator">What is the advantage of a comma operator</a></td></tr><tr><td>251</td><td><a href="#what-is-typescript">What is typescript</a></td></tr><tr><td>252</td><td><a href="#what-are-the-differences-between-javascript-and-typescript">What are the differences between javascript and typescript</a></td></tr><tr><td>253</td><td><a href="#what-are-the-advantages-of-typescript-over-javascript">What are the advantages of typescript over javascript</a></td></tr><tr><td>254</td><td><a href="#what-is-an-object-initializer">What is an object initializer</a></td></tr><tr><td>255</td><td><a href="#what-is-a-constructor-method">What is a constructor method</a></td></tr><tr><td>256</td><td><a href="#what-happens-if-you-write-constructor-more-than-once-in-a-class">What happens if you write constructor more than once in a class</a></td></tr><tr><td>257</td><td><a href="#how-do-you-call-the-constructor-of-a-parent-class">How do you call the constructor of a parent class</a></td></tr><tr><td>258</td><td><a href="#how-do-you-get-the-prototype-of-an-object">How do you get the prototype of an object</a></td></tr><tr><td>259</td><td><a href="#what-happens-if-i-pass-string-type-for-getprototype-method">What happens If I pass string type for getPrototype method</a></td></tr><tr><td>260</td><td><a href="#how-do-you-set-prototype-of-one-object-to-another">How do you set prototype of one object to another</a></td></tr><tr><td>261</td><td><a href="#how-do-you-check-whether-an-object-can-be-extendable-or-not">How do you check whether an object can be extendable or not</a></td></tr><tr><td>262</td><td><a href="#how-do-you-prevent-an-object-to-extend">How do you prevent an object to extend</a></td></tr><tr><td>263</td><td><a href="#what-are-the-different-ways-to-make-an-object-non-extensible">What are the different ways to make an object non-extensible</a></td></tr><tr><td>264</td><td><a href="#how-do-you-define-multiple-properties-on-an-object">How do you define multiple properties on an object</a></td></tr><tr><td>265</td><td><a href="#what-is-mean-in-javascript">What is MEAN in javascript</a></td></tr><tr><td>266</td><td><a href="#what-is-obfuscation-in-javascript">What Is Obfuscation in javascript</a></td></tr><tr><td>267</td><td><a href="#why-do-you-need-obfuscation">Why do you need Obfuscation</a></td></tr><tr><td>268</td><td><a href="#what-is-minification">What is Minification</a></td></tr><tr><td>269</td><td><a href="#what-are-the-advantages-of-minification">What are the advantages of minification</a></td></tr><tr><td>270</td><td><a href="#what-are-the-differences-between-obfuscation-and-encryption">What are the differences between Obfuscation and Encryption</a></td></tr><tr><td>271</td><td><a href="#what-are-the-common-tools-used-for-minification">What are the common tools used for minification</a></td></tr><tr><td>272</td><td><a href="#how-do-you-perform-form-validation-using-javascript">How do you perform form validation using javascript</a></td></tr><tr><td>273</td><td><a href="#how-do-you-perform-form-validation-without-javascript">How do you perform form validation without javascript</a></td></tr><tr><td>274</td><td><a href="#what-are-the-dom-methods-available-for-constraint-validation">What are the DOM methods available for constraint validation</a></td></tr><tr><td>275</td><td><a href="#what-are-the-available-constraint-validation-dom-properties">What are the available constraint validation DOM properties</a></td></tr><tr><td>276</td><td><a href="#what-are-the-list-of-validity-properties">What are the list of validity properties</a></td></tr><tr><td>277</td><td><a href="#give-an-example-usage-of-rangeoverflow-property">Give an example usage of rangeOverflow property</a></td></tr><tr><td>278</td><td><a href="#is-enums-feature-available-in-javascript">Is enums feature available in javascript</a></td></tr><tr><td>279</td><td><a href="#What-is-an-enum">What is an enum</a></td></tr><tr><td>280</td><td><a href="#how-do-you-list-all-properties-of-an-object">How do you list all properties of an object</a></td></tr><tr><td>281</td><td><a href="#how-do-you-get-property-descriptors-of-an-object">How do you get property descriptors of an object</a></td></tr><tr><td>282</td><td><a href="#what-are-the-attributes-provided-by-a-property-descriptor">What are the attributes provided by a property descriptor</a></td></tr><tr><td>283</td><td><a href="#how-do-you-extend-classes">How do you extend classes</a></td></tr><tr><td>284</td><td><a href="#how-do-i-modify-the-url-without-reloading-the-page">How do I modify the url without reloading the page</a></td></tr><tr><td>285</td><td><a href="#how-do-you-check-whether-an-array-includes-a-particular-value-or-not">How do you check whether an array includes a particular value or not</a></td></tr><tr><td>286</td><td><a href="#how-do-you-compare-scalar-arrays">How do you compare scalar arrays</a></td></tr><tr><td>287</td><td><a href="#how-to-get-the-value-from-get-parameters">How to get the value from get parameters</a></td></tr><tr><td>288</td><td><a href="#how-do-you-print-numbers-with-commas-as-thousand-separators">How do you print numbers with commas as thousand separators</a></td></tr><tr><td>289</td><td><a href="#what-is-the-difference-between-java-and-javascript">What is the difference between java and javascript</a></td></tr><tr><td>290</td><td><a href="#is-javascript-supports-namespace">Is javascript supports namespace</a></td></tr><tr><td>291</td><td><a href="#how-do-you-declare-namespace">How do you declare namespace</a></td></tr><tr><td>292</td><td><a href="#how-do-you-invoke-javascript-code-in-an-iframe-from-parent-page">How do you invoke javascript code in an iframe from parent page</a></td></tr><tr><td>293</td><td><a href="#how-do-get-the-timezone-offset-from-date">How do get the timezone offset from date</a></td></tr><tr><td>294</td><td><a href="#how-do-you-load-css-and-js-files-dynamically">How do you load CSS and JS files dynamically</a></td></tr><tr><td>295</td><td><a href="#what-are-the-different-methods-to-find-html-elements-in-dom">What are the different methods to find HTML elements in DOM</a></td></tr><tr><td>296</td><td><a href="#what-is-jquery">What is jQuery</a></td></tr><tr><td>297</td><td><a href="#what-is-v8-javascript-engine">What is V8 JavaScript engine</a></td></tr><tr><td>298</td><td><a href="#why-do-we-call-javascript-as-dynamic-language">Why do we call javascript as dynamic language</a></td></tr><tr><td>299</td><td><a href="#what-is-a-void-operator">What is a void operator</a></td></tr><tr><td>300</td><td><a href="#how-to-set-the-cursor-to-wait">How to set the cursor to wait</a></td></tr><tr><td>301</td><td><a href="#how-do-you-create-an-infinite-loop">How do you create an infinite loop</a></td></tr><tr><td>302</td><td><a href="#why-do-you-need-to-avoid-with-statement">Why do you need to avoid with statement</a></td></tr><tr><td>303</td><td><a href="#what-is-the-output-of-below-for-loops">What is the output of below for loops</a></td></tr><tr><td>304</td><td><a href="#list-down-some-of-the-features-of-es6">List down some of the features of ES6</a></td></tr><tr><td>305</td><td><a href="#what-is-es6">What is ES6</a></td></tr><tr><td>306</td><td><a href="#can-I-redeclare-let-and-const-variables">Can I redeclare let and const variables</a></td></tr><tr><td>307</td><td><a href="#is-const-variable-makes-the-value-immutable">Is const variable makes the value immutable</a></td></tr><tr><td>308</td><td><a href="#what-are-default-parameters">What are default parameters</a></td></tr><tr><td>309</td><td><a href="#what-are-template-literals">What are template literals</a></td></tr><tr><td>310</td><td><a href="#how-do-you-write-multi-line-strings-in-template-literals">How do you write multi-line strings in template literals</a></td></tr><tr><td>311</td><td><a href="#what-are-nesting-templates">What are nesting templates</a></td></tr><tr><td>312</td><td><a href="#what-are-tagged-templates">What are tagged templates</a></td></tr><tr><td>313</td><td><a href="#what-are-raw-strings">What are raw strings</a></td></tr><tr><td>314</td><td><a href="#what-is-destructuring-assignment">What is destructuring assignment</a></td></tr><tr><td>315</td><td><a href="#what-are-default-values-in-destructuring-assignment">What are default values in destructuring assignment</a></td></tr><tr><td>316</td><td><a href="#how-do-you-swap-variables-in-destructuring-assignment">How do you swap variables in destructuring assignment</a></td></tr><tr><td>317</td><td><a href="#what-are-enhanced-object-literals">What are enhanced object literals</a></td></tr><tr><td>318</td><td><a href="#what-are-dynamic-imports">What are dynamic imports</a></td></tr><tr><td>319</td><td><a href="#what-are-the-use-cases-for-dynamic-imports">What are the use cases for dynamic imports</a></td></tr><tr><td>320</td><td><a href="#what-are-typed-arrays">What are typed arrays</a></td></tr><tr><td>321</td><td><a href="#what-are-the-advantages-of-module-loaders">What are the advantages of module loaders</a></td></tr><tr><td>322</td><td><a href="#what-is-collation">What is collation</a></td></tr><tr><td>323</td><td><a href="#what-is-for...of-statement">What is for…of statement</a></td></tr><tr><td>324</td><td><a href="#what-is-the-output-of-below-spread-operator-array">What is the output of below spread operator array</a></td></tr><tr><td>325</td><td><a href="#is-postmessage-secure">Is PostMessage secure</a></td></tr><tr><td>326</td><td><a href="#what-are-the-problems-with-postmessage-target-origin-as-wildcard">What are the problems with postmessage target origin as wildcard</a></td></tr><tr><td>327</td><td><a href="#how-do-you-avoid-receiving-postmessages-from-attackers">How do you avoid receiving postMessages from attackers</a></td></tr><tr><td>328</td><td><a href="#can-i-avoid-using-postmessages-completely">Can I avoid using postMessages completely</a></td></tr><tr><td>329</td><td><a href="#is-postmessages-synchronous">Is postMessages synchronous</a></td></tr><tr><td>330</td><td><a href="#what-paradigm-is-javascript">What paradigm is Javascript</a></td></tr><tr><td>331</td><td><a href="#what-is-the-difference-between-internal-and-external-javascript">What is the difference between internal and external javascript</a></td></tr><tr><td>332</td><td><a href="#is-javascript-faster-than-server-side-script">Is JavaScript faster than server side script</a></td></tr><tr><td>333</td><td><a href="#how-do-you-get-the-status-of-a-checkbox">How do you get the status of a checkbox</a></td></tr><tr><td>334</td><td><a href="#what-is-the-purpose-of-double-tilde-operator">What is the purpose of double tilde operator</a></td></tr><tr><td>335</td><td><a href="#how-do-you-convert-character-to-ascii-code">How do you convert character to ASCII code</a></td></tr><tr><td>336</td><td><a href="#what-is-arraybuffer">What is ArrayBuffer</a></td></tr><tr><td>337</td><td><a href="#what-is-the-output-of-below-string-expression">What is the output of below string expression</a></td></tr><tr><td>338</td><td><a href="#what-is-the-purpose-of-error-object">What is the purpose of Error object</a></td></tr><tr><td>339</td><td><a href="#what-is-the-purpose-of-evalerror-object">What is the purpose of EvalError object</a></td></tr><tr><td>340</td><td><a href="#what-are-the-list-of-cases-error-thrown-from-non-strict-mode-to-strict-mode">What are the list of cases error thrown from non-strict mode to strict mode</a></td></tr><tr><td>341</td><td><a href="#is-all-objects-have-prototypes">Is all objects have prototypes</a></td></tr><tr><td>342</td><td><a href="#what-is-the-difference-between-a-parameter-and-an-argument">What is the difference between a parameter and an argument</a></td></tr><tr><td>343</td><td><a href="#what-is-the-purpose-of-some-method-in-arrays">What is the purpose of some method in arrays</a></td></tr><tr><td>344</td><td><a href="#how-do-you-combine-two-or-more-arrays">How do you combine two or more arrays</a></td></tr><tr><td>345</td><td><a href="#what-is-the-difference-between-shallow-and-deep-copy">What is the difference between Shallow and Deep copy</a></td></tr><tr><td>346</td><td><a href="#how-do-you-create-specific-number-of-copies-of-a-string">How do you create specific number of copies of a string</a></td></tr><tr><td>347</td><td><a href="#how-do-you-return-all-matching-strings-against-a-regular-expression">How do you return all matching strings against a regular expression</a></td></tr><tr><td>348</td><td><a href="#how-do-you-trim-a-string-at-the-beginning-or-ending">How do you trim a string at the beginning or ending</a></td></tr><tr><td>349</td><td><a href="#what-is-the-output-of-below-console-statement-with-unary-operator">What is the output of below console statement with unary operator</a></td></tr><tr><td>350</td><td><a href="#does-javascript-uses-mixins">Does javascript uses mixins</a></td></tr><tr><td>351</td><td><a href="#what-is-a-thunk-function">What is a thunk function</a></td></tr><tr><td>352</td><td><a href="#what-are-asynchronous-thunks">What are asynchronous thunks</a></td></tr><tr><td>353</td><td><a href="#what-is-the-output-of-below-function-calls">What is the output of below function calls</a></td></tr><tr><td>354</td><td><a href="#how-to-remove-all-line-breaks-from-a-string">How to remove all line breaks from a string</a></td></tr><tr><td>355</td><td><a href="#what-is-the-difference-between-reflow-and-repaint">What is the difference between reflow and repaint</a></td></tr><tr><td>356</td><td><a href="#what-happens-with-negating-an-array">What happens with negating an array</a></td></tr><tr><td>357</td><td><a href="#what-happens-if-we-add-two-arrays">What happens if we add two arrays</a></td></tr><tr><td>358</td><td><a href="#what-is-the-output-of-prepend-additive-operator-on-falsy-values">What is the output of prepend additive operator on falsy values</a></td></tr><tr><td>359</td><td><a href="#how-do-you-create-self-string-using-special-characters">How do you create self string using special characters</a></td></tr><tr><td>360</td><td><a href="#how-do-you-remove-falsy-values-from-an-array">How do you remove falsy values from an array</a></td></tr><tr><td>361</td><td><a href="#how-do-you-get-unique-values-of-an-array">How do you get unique values of an array</a></td></tr><tr><td>362</td><td><a href="#what-is-destructuring-aliases">What is destructuring aliases</a></td></tr><tr><td>363</td><td><a href="#how-do-you-map-the-array-values-without-using-map-method">How do you map the array values without using map method</a></td></tr><tr><td>364</td><td><a href="#how-do-you-empty-an-array">How do you empty an array</a></td></tr><tr><td>365</td><td><a href="#how-do-you-rounding-numbers-to-certain-decimals">How do you rounding numbers to certain decimals</a></td></tr><tr><td>366</td><td><a href="#what-is-the-easiest-way-to-convert-an-array-to-an-object">What is the easiest way to convert an array to an object</a></td></tr><tr><td>367</td><td><a href="#how-do-you-create-an-array-with-some-data">How do you create an array with some data</a></td></tr><tr><td>368</td><td><a href="#what-are-the-placeholders-from-console-object">What are the placeholders from console object</a></td></tr><tr><td>369</td><td><a href="#is-it-possible-to-add-css-to-console-messages">Is it possible to add CSS to console messages</a></td></tr><tr><td>370</td><td><a href="#what-is-the-purpose-of-dir-method-of-console-object">What is the purpose of dir method of console object</a></td></tr><tr><td>371</td><td><a href="#is-it-possible-to-debug-html-elements-in-console">Is it possible to debug HTML elements in console</a></td></tr><tr><td>372</td><td><a href="#how-do-you-display-data-in-a-tabular-format-using-console-object">How do you display data in a tabular format using console object</a></td></tr><tr><td>373</td><td><a href="#how-do-you-verify-that-an-argument-is-a-number-or-not">How do you verify that an argument is a Number or not</a></td></tr><tr><td>374</td><td><a href="#how-do-you-create-copy-to-clipboard-button">How do you create copy to clipboard button</a></td></tr><tr><td>375</td><td><a href="#what-is-the-shortcut-to-get-timestamp">What is the shortcut to get timestamp</a></td></tr><tr><td>376</td><td><a href="#how-do-you-flattening-multi-dimensional-arrays">How do you flattening multi dimensional arrays</a></td></tr><tr><td>377</td><td><a href="#what-is-the-easiest-multi-condition-checking">What is the easiest multi condition checking</a></td></tr><tr><td>378</td><td><a href="#how-do-you-capture-browser-back-button">How do you capture browser back button</a></td></tr><tr><td>379</td><td><a href="#how-do-you-disable-right-click-in-the-web-page">How do you disable right click in the web page</a></td></tr><tr><td>380</td><td><a href="#what-are-wrapper-objects">What are wrapper objects</a></td></tr><tr><td>381</td><td><a href="#what-is-ajax">What is AJAX</a></td></tr><tr><td>382</td><td><a href="#what-are-the-different-ways-to-deal-with-asynchronous-code">What are the different ways to deal with Asynchronous Code</a></td></tr><tr><td>383</td><td><a href="#how-to-cancel-a-fetch-request">How to cancel a fetch request</a></td></tr><tr><td>384</td><td><a href="#what-is-web-speech-api">What is web speech API</a></td></tr><tr><td>385</td><td><a href="#what-is-minimum-timeout-throttling">What is minimum timeout throttling</a></td></tr><tr><td>386</td><td><a href="#how-do-you-implement-zero-timeout-in-modern-browsers">How do you implement zero timeout in modern browsers</a></td></tr><tr><td>387</td><td><a href="#what-are-tasks-in-event-loop">What are tasks in event loop</a></td></tr><tr><td>388</td><td><a href="#what-are-microtasks">What are microtasks</a></td></tr><tr><td>389</td><td><a href="#what-are-different-event-loops">What are different event loops</a></td></tr><tr><td>390</td><td><a href="#what-is-the-purpose-of-queuemicrotask">What is the purpose of queueMicrotask</a></td></tr><tr><td>391</td><td><a href="#how-do-you-use-javascript-libraries-in-typescript-file">How do you use javascript libraries in typescript file</a></td></tr><tr><td>392</td><td><a href="#what-are-the-differences-between-promises-and-observables">What are the differences between promises and observables</a></td></tr><tr><td>393</td><td><a href="#what-is-heap">What is heap</a></td></tr><tr><td>394</td><td><a href="#what-is-an-event-table">What is an event table</a></td></tr><tr><td>395</td><td><a href="#what-is-a-microtask-queue">What is a microTask queue</a></td></tr><tr><td>396</td><td><a href="#what-is-the-difference-between-shim-and-polyfill">What is the difference between shim and polyfill</a></td></tr><tr><td>397</td><td><a href="#how-do-you-detect-primitive-or-non-primitive-value-type">How do you detect primitive or non primitive value type</a></td></tr><tr><td>398</td><td><a href="#what-is-babel">What is babel</a></td></tr><tr><td>399</td><td><a href="#is-node.js-completely-single-threaded">Is Node.js completely single threaded</a></td></tr><tr><td>400</td><td><a href="#what-are-the-common-use-cases-of-observables">What are the common use cases of observables</a></td></tr><tr><td>401</td><td><a href="#what-is-rxjs">What is RxJS</a></td></tr><tr><td>402</td><td><a href="#what-is-the-difference-between-function-constructor-and-function-declaration">What is the difference between Function constructor and function declaration</a></td></tr><tr><td>403</td><td><a href="#what-is-a-short-circuit-condition">What is a Short circuit condition</a></td></tr><tr><td>404</td><td><a href="#what-is-the-easiest-way-to-resize-an-array">What is the easiest way to resize an array</a></td></tr><tr><td>405</td><td><a href="#what-is-an-observable">What is an observable</a></td></tr><tr><td>406</td><td><a href="#what-is-the-difference-between-function-and-class-declarations">What is the difference between function and class declarations</a></td></tr><tr><td>407</td><td><a href="#what-is-an-async-function">What is an async function</a></td></tr><tr><td>408</td><td><a href="#how-do-you-prevent-promises-swallowing-errors">How do you prevent promises swallowing errors</a></td></tr><tr><td>409</td><td><a href="#what-is-deno">What is deno</a></td></tr><tr><td>410</td><td><a href="#how-do-you-make-an-object-iterable-in-javascript">How do you make an object iterable in javascript</a></td></tr><tr><td>411</td><td><a href="#what-is-a-proper-tail-call">What is a Proper Tail Call</a></td></tr><tr><td>412</td><td><a href="#how-do-you-check-an-object-is-a-promise-or-not">How do you check an object is a promise or not</a></td></tr><tr><td>413</td><td><a href="#how-to-detect-if-a-function-is-called-as-constructor">How to detect if a function is called as constructor</a></td></tr><tr><td>414</td><td><a href="#what-are-the-differences-between-arguments-object-and-rest-parameter">What are the differences between arguments object and rest parameter</a></td></tr><tr><td>415</td><td><a href="#what-are-the-differences-between-spread-operator-and-rest-parameter">What are the differences between spread operator and rest parameter</a></td></tr><tr><td>416</td><td><a href="#what-are-the-different-kinds-of-generators">What are the different kinds of generators</a></td></tr><tr><td>417</td><td><a href="#what-are-the-built-in-iterables">What are the built-in iterables</a></td></tr><tr><td>418</td><td><a href="#what-are-the-differences-between-for...of-and-for...in-statements">What are the differences between for…of and for…in statements</a></td></tr><tr><td>419</td><td><a href="#how-do-you-define-instance-and-non-instance-properties">How do you define instance and non-instance properties</a></td></tr><tr><td>420</td><td><a href="#what-is-the-difference-between-isnan-and-number.isnan">What is the difference between isNaN and Number.isNaN?</a></td></tr></tbody></table><hr><ol><li><h3 id="JS-创建对象有那些方法"><a href="#JS-创建对象有那些方法" class="headerlink" title="JS 创建对象有那些方法"></a>JS 创建对象有那些方法</h3><p>在javascript中有很多方法可以创建对象，如下所示</p><ol><li><p><strong>Object的构造函数:</strong></p><p>创建空对象最简单的方法是使用object构造函数。目前不推荐使用这种方法。</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> object = <span class="keyword">new</span> <span class="built_in">Object</span>();</span><br></pre></td></tr></table></figure></li><li><p><strong>Object’s 创建方法:</strong></p><p>Object的create方法通过将prototype对象作为参数传递来创建一个新对象<br><em>比较纯粹的对象，没有原型链</em></p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> object = <span class="built_in">Object</span>.create(<span class="literal">null</span>);</span><br></pre></td></tr></table></figure></li><li><p><strong>Object字面语法:</strong></p><p>object字面量语法等价于create method，当它传递null作为参数时</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> object = &#123;&#125;;</span><br></pre></td></tr></table></figure></li><li><p><strong>构造函数:</strong></p><p>创建任何函数并应用new操作符来创建对象实例</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">Person</span>(<span class="params">name</span>)</span>&#123;</span><br><span class="line">   <span class="keyword">var</span> object = &#123;&#125;;</span><br><span class="line">   object.name=name;</span><br><span class="line">   object.age=<span class="number">21</span>;</span><br><span class="line">   <span class="keyword">return</span> object;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">var</span> object = <span class="keyword">new</span> Person(<span class="string">&quot;Sudheer&quot;</span>);</span><br></pre></td></tr></table></figure></li><li><p><strong>带原型的函数构造函数:</strong></p><p>这与函数构造函数类似，但它们的属性和方法使用了prototype</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">Person</span>(<span class="params"></span>)</span>&#123;&#125;</span><br><span class="line">Person.prototype.name = <span class="string">&quot;Sudheer&quot;</span>;</span><br><span class="line"><span class="keyword">var</span> object = <span class="keyword">new</span> Person();</span><br></pre></td></tr></table></figure><p>这相当于用一个对象创建方法和一个函数原型创建一个实例，然后用一个实例和参数作为参数调用该函数。</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">func</span> </span>&#123;&#125;;</span><br><span class="line"></span><br><span class="line"><span class="keyword">new</span> func(x, y, z);</span><br></pre></td></tr></table></figure><p><strong>(或者)</strong></p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 使用function prototype创建一个新的实例。</span></span><br><span class="line"><span class="keyword">var</span> newInstance = <span class="built_in">Object</span>.create(func.prototype)</span><br><span class="line"></span><br><span class="line"><span class="comment">// Call the function</span></span><br><span class="line"><span class="keyword">var</span> result = func.call(newInstance, x, y, z),</span><br><span class="line"></span><br><span class="line"><span class="comment">// 如果结果是一个非空对象，那么使用它，否则只使用新实例。</span></span><br><span class="line"><span class="built_in">console</span>.log(result &amp;&amp; <span class="keyword">typeof</span> result === <span class="string">&#x27;object&#x27;</span> ? result : newInstance);</span><br></pre></td></tr></table></figure></li><li><p><strong>ES6 Class 语法:</strong></p><p>ES6引入了类特性来创建对象</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Person</span> </span>&#123;</span><br><span class="line">   <span class="function"><span class="title">constructor</span>(<span class="params">name</span>)</span> &#123;</span><br><span class="line">      <span class="built_in">this</span>.name = name;</span><br><span class="line">   &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> object = <span class="keyword">new</span> Person(<span class="string">&quot;Sudheer&quot;</span>);</span><br></pre></td></tr></table></figure></li><li><p><strong>单例模式:</strong></p><p>单例对象是只能实例化一次的对象。重复调用其构造函数将返回相同的实例，这样可以确保它们不会意外地创建多个实例。</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> object = <span class="keyword">new</span> <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">   <span class="built_in">this</span>.name = <span class="string">&quot;Sudheer&quot;</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li></ol></li><li><h3 id="什么是原型链"><a href="#什么是原型链" class="headerlink" title="什么是原型链"></a>什么是原型链</h3><p><strong>Prototype chaining</strong> 用于在现有对象的基础上构建新的对象类型。它类似于基于类的语言中的继承. 对象实例上的原型可以通过 <strong>Object.getPrototypeOf(object)</strong> or <strong>proto</strong> 而constructor上的prototype函数可以通过object.prototype.</p><p><img src="/images/prototype_chain.png" alt="Screenshot"></p><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="Call、Apply、Bind-之间的区别"><a href="#Call、Apply、Bind-之间的区别" class="headerlink" title="Call、Apply、Bind 之间的区别"></a>Call、Apply、Bind 之间的区别</h3><p>调用、应用和绑定之间的区别可以用下面的例子来解释</p><p><strong>Call:</strong> call() 方法调用给定的函数 <code>this</code> 一个接一个地提供值和参数</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> employee1 = &#123;<span class="attr">firstName</span>: <span class="string">&#x27;John&#x27;</span>, <span class="attr">lastName</span>: <span class="string">&#x27;Rodson&#x27;</span>&#125;;</span><br><span class="line"><span class="keyword">var</span> employee2 = &#123;<span class="attr">firstName</span>: <span class="string">&#x27;Jimmy&#x27;</span>, <span class="attr">lastName</span>: <span class="string">&#x27;Baily&#x27;</span>&#125;;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">invite</span>(<span class="params">greeting1, greeting2</span>) </span>&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(greeting1 + <span class="string">&#x27; &#x27;</span> + <span class="built_in">this</span>.firstName + <span class="string">&#x27; &#x27;</span> + <span class="built_in">this</span>.lastName+ <span class="string">&#x27;, &#x27;</span>+ greeting2);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">invite.call(employee1, <span class="string">&#x27;Hello&#x27;</span>, <span class="string">&#x27;How are you?&#x27;</span>); <span class="comment">// Hello John Rodson, How are you?</span></span><br><span class="line">invite.call(employee2, <span class="string">&#x27;Hello&#x27;</span>, <span class="string">&#x27;How are you?&#x27;</span>); <span class="comment">// Hello Jimmy Baily, How are you?</span></span><br></pre></td></tr></table></figure><p><strong>Apply:</strong> 调用函数并允许您将参数作为数组传递</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> employee1 = &#123;<span class="attr">firstName</span>: <span class="string">&#x27;John&#x27;</span>, <span class="attr">lastName</span>: <span class="string">&#x27;Rodson&#x27;</span>&#125;;</span><br><span class="line"><span class="keyword">var</span> employee2 = &#123;<span class="attr">firstName</span>: <span class="string">&#x27;Jimmy&#x27;</span>, <span class="attr">lastName</span>: <span class="string">&#x27;Baily&#x27;</span>&#125;;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">invite</span>(<span class="params">greeting1, greeting2</span>) </span>&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(greeting1 + <span class="string">&#x27; &#x27;</span> + <span class="built_in">this</span>.firstName + <span class="string">&#x27; &#x27;</span> + <span class="built_in">this</span>.lastName+ <span class="string">&#x27;, &#x27;</span>+ greeting2);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">invite.apply(employee1, [<span class="string">&#x27;Hello&#x27;</span>, <span class="string">&#x27;How are you?&#x27;</span>]); <span class="comment">// Hello John Rodson, How are you?</span></span><br><span class="line">invite.apply(employee2, [<span class="string">&#x27;Hello&#x27;</span>, <span class="string">&#x27;How are you?&#x27;</span>]); <span class="comment">// Hello Jimmy Baily, How are you?</span></span><br></pre></td></tr></table></figure><p><strong>bind:</strong> 返回一个新函数，允许传入一个数组和任意数量的参数</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> employee1 = &#123;<span class="attr">firstName</span>: <span class="string">&#x27;John&#x27;</span>, <span class="attr">lastName</span>: <span class="string">&#x27;Rodson&#x27;</span>&#125;;</span><br><span class="line"><span class="keyword">var</span> employee2 = &#123;<span class="attr">firstName</span>: <span class="string">&#x27;Jimmy&#x27;</span>, <span class="attr">lastName</span>: <span class="string">&#x27;Baily&#x27;</span>&#125;;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">invite</span>(<span class="params">greeting1, greeting2</span>) </span>&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(greeting1 + <span class="string">&#x27; &#x27;</span> + <span class="built_in">this</span>.firstName + <span class="string">&#x27; &#x27;</span> + <span class="built_in">this</span>.lastName+ <span class="string">&#x27;, &#x27;</span>+ greeting2);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> inviteEmployee1 = invite.bind(employee1);</span><br><span class="line"><span class="keyword">var</span> inviteEmployee2 = invite.bind(employee2);</span><br><span class="line">inviteEmployee1(<span class="string">&#x27;Hello&#x27;</span>, <span class="string">&#x27;How are you?&#x27;</span>); <span class="comment">// Hello John Rodson, How are you?</span></span><br><span class="line">inviteEmployee2(<span class="string">&#x27;Hello&#x27;</span>, <span class="string">&#x27;How are you?&#x27;</span>); <span class="comment">// Hello Jimmy Baily, How are you?</span></span><br></pre></td></tr></table></figure><p>Call and apply are pretty interchangeable. Both execute the current function immediately. You need to decide whether it’s easier to send in an array or a comma separated list of arguments. You can remember by treating Call is for comma (separated list) and Apply is for Array. Whereas Bind creates a new function that will have <code>this</code> set to the first parameter passed to bind().</p><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-JSON-and-its-common-operations"><a href="#What-is-JSON-and-its-common-operations" class="headerlink" title="What is JSON and its common operations"></a>What is JSON and its common operations</h3><p><strong>JSON</strong> is a text-based data format following JavaScript object syntax, which was popularized by <code>Douglas Crockford</code>. It is useful when you want to transmit data across a network and it is basically just a text file with an extension of .json, and a MIME type of application/json<br><strong>Parsing:</strong> Converting a string to a native object</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">JSON</span>.parse(text)</span><br></pre></td></tr></table></figure><p>Stringification: **converting a native object to a string so it can be transmitted across the network</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">JSON</span>.stringify(object)</span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-the-purpose-of-the-array-slice-method"><a href="#What-is-the-purpose-of-the-array-slice-method" class="headerlink" title="What is the purpose of the array slice method"></a>What is the purpose of the array slice method</h3><p>The <strong>slice()</strong> method returns the selected elements in an array as a new array object. It selects the elements starting at the given start argument, and ends at the given optional end argument without including the last element. If you omit the second argument then it selects till the end. Some of the examples of this method are,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> arrayIntegers = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>];</span><br><span class="line"><span class="keyword">let</span> arrayIntegers1 = arrayIntegers.slice(<span class="number">0</span>,<span class="number">2</span>); <span class="comment">// returns [1,2]</span></span><br><span class="line"><span class="keyword">let</span> arrayIntegers2 = arrayIntegers.slice(<span class="number">2</span>,<span class="number">3</span>); <span class="comment">// returns [3]</span></span><br><span class="line"><span class="keyword">let</span> arrayIntegers3 = arrayIntegers.slice(<span class="number">4</span>); <span class="comment">//returns [5]</span></span><br></pre></td></tr></table></figure><p><strong>Note:</strong> Slice method won’t mutate the original array but it returns the subset as a new array.</p><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-the-purpose-of-the-array-splice-method"><a href="#What-is-the-purpose-of-the-array-splice-method" class="headerlink" title="What is the purpose of the array splice method"></a>What is the purpose of the array splice method</h3><p>The <strong>splice()</strong> method is used either adds/removes items to/from an array, and then returns the removed item. The first argument specifies the array position for insertion or deletion whereas the option second argument indicates the number of elements to be deleted. Each additional argument is added to the array. Some of the examples of this method are,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> arrayIntegersOriginal1 = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>];</span><br><span class="line"><span class="keyword">let</span> arrayIntegersOriginal2 = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>];</span><br><span class="line"><span class="keyword">let</span> arrayIntegersOriginal3 = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>];</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> arrayIntegers1 = arrayIntegersOriginal1.splice(<span class="number">0</span>,<span class="number">2</span>); <span class="comment">// returns [1, 2]; original array: [3, 4, 5]</span></span><br><span class="line"><span class="keyword">let</span> arrayIntegers2 = arrayIntegersOriginal2.splice(<span class="number">3</span>); <span class="comment">// returns [4, 5]; original array: [1, 2, 3]</span></span><br><span class="line"><span class="keyword">let</span> arrayIntegers3 = arrayIntegersOriginal3.splice(<span class="number">3</span>, <span class="number">1</span>, <span class="string">&quot;a&quot;</span>, <span class="string">&quot;b&quot;</span>, <span class="string">&quot;c&quot;</span>); <span class="comment">//returns [4]; original array: [1, 2, 3, &quot;a&quot;, &quot;b&quot;, &quot;c&quot;, 5]</span></span><br></pre></td></tr></table></figure><p><strong>Note:</strong> Splice method modifies the original array and returns the deleted array.</p><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-the-difference-between-slice-and-splice"><a href="#What-is-the-difference-between-slice-and-splice" class="headerlink" title="What is the difference between slice and splice"></a>What is the difference between slice and splice</h3><p>Some of the major difference in a tabular form</p><table><thead><tr><th>Slice</th><th>Splice</th></tr></thead><tbody><tr><td>Doesn’t modify the original array(immutable)</td><td>Modifies the original array(mutable)</td></tr><tr><td>Returns the subset of original array</td><td>Returns the deleted elements as array</td></tr><tr><td>Used to pick the elements from array</td><td>Used to insert or delete elements to/from array</td></tr></tbody></table><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="How-do-you-compare-Object-and-Map"><a href="#How-do-you-compare-Object-and-Map" class="headerlink" title="How do you compare Object and Map"></a>How do you compare Object and Map</h3><p><strong>Objects</strong> are similar to <strong>Maps</strong> in that both let you set keys to values, retrieve those values, delete keys, and detect whether something is stored at a key. Due to this reason, Objects have been used as Maps historically. But there are important differences that make using a Map preferable in certain cases.</p><ol><li>The keys of an Object are Strings and Symbols, whereas they can be any value for a Map, including functions, objects, and any primitive.</li><li>The keys in Map are ordered while keys added to Object are not. Thus, when iterating over it, a Map object returns keys in order of insertion.</li><li>You can get the size of a Map easily with the size property, while the number of properties in an Object must be determined manually.</li><li>A Map is an iterable and can thus be directly iterated, whereas iterating over an Object requires obtaining its keys in some fashion and iterating over them.</li><li>An Object has a prototype, so there are default keys in the map that could collide with your keys if you’re not careful. As of ES5 this can be bypassed by using map = Object.create(null), but this is seldom done.</li><li>A Map may perform better in scenarios involving frequent addition and removal of key pairs.</li></ol><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-the-difference-between-and-operators"><a href="#What-is-the-difference-between-and-operators" class="headerlink" title="What is the difference between == and === operators"></a>What is the difference between == and === operators</h3><p>JavaScript provides both strict(===, !==) and type-converting(==, !=) equality comparison. The strict operators take type of variable in consideration, while non-strict operators make type correction/conversion based upon values of variables. The strict operators follow the below conditions for different types,</p><ol><li><p>Two strings are strictly equal when they have the same sequence of characters, same length, and same characters in corresponding positions.</p></li><li><p>Two numbers are strictly equal when they are numerically equal. i.e, Having the same number value.<br>There are two special cases in this,</p><ol><li>NaN is not equal to anything, including NaN.</li><li>Positive and negative zeros are equal to one another.</li></ol></li><li><p>Two Boolean operands are strictly equal if both are true or both are false.</p></li><li><p>Two objects are strictly equal if they refer to the same Object.</p></li><li><p>Null and Undefined types are not equal with ===, but equal with ==. i.e,<br>null===undefined –&gt; false but null==undefined –&gt; true</p><p>Some of the example which covers the above cases,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">0</span> == <span class="literal">false</span>   <span class="comment">// true</span></span><br><span class="line"><span class="number">0</span> === <span class="literal">false</span>  <span class="comment">// false</span></span><br><span class="line"><span class="number">1</span> == <span class="string">&quot;1&quot;</span>     <span class="comment">// true</span></span><br><span class="line"><span class="number">1</span> === <span class="string">&quot;1&quot;</span>    <span class="comment">// false</span></span><br><span class="line"><span class="literal">null</span> == <span class="literal">undefined</span> <span class="comment">// true</span></span><br><span class="line"><span class="literal">null</span> === <span class="literal">undefined</span> <span class="comment">// false</span></span><br><span class="line"><span class="string">&#x27;0&#x27;</span> == <span class="literal">false</span> <span class="comment">// true</span></span><br><span class="line"><span class="string">&#x27;0&#x27;</span> === <span class="literal">false</span> <span class="comment">// false</span></span><br><span class="line">[]==[] or []===[] <span class="comment">//false, refer different objects in memory</span></span><br><span class="line">&#123;&#125;==&#123;&#125; or &#123;&#125;===&#123;&#125; <span class="comment">//false, refer different objects in memory</span></span><br></pre></td></tr></table></figure></li></ol><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-are-lambda-or-arrow-functions"><a href="#What-are-lambda-or-arrow-functions" class="headerlink" title="What are lambda or arrow functions"></a>What are lambda or arrow functions</h3><p>An arrow function is a shorter syntax for a function expression and does not have its own <strong>this, arguments, super, or new.target</strong>. These functions are best suited for non-method functions, and they cannot be used as constructors.</p><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-a-first-class-function"><a href="#What-is-a-first-class-function" class="headerlink" title="What is a first class function"></a>What is a first class function</h3><p>In Javascript, functions are first class objects. First-class functions means when functions in that language are treated like any other variable.</p><p>For example, in such a language, a function can be passed as an argument to other functions, can be returned by another function and can be assigned as a value to a variable. For example, in the below example, handler functions assigned to a listener</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> handler = <span class="function">() =&gt;</span> <span class="built_in">console</span>.log (<span class="string">&#x27;This is a click handler function&#x27;</span>);</span><br><span class="line"><span class="built_in">document</span>.addEventListener (<span class="string">&#x27;click&#x27;</span>, handler);</span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-a-first-order-function"><a href="#What-is-a-first-order-function" class="headerlink" title="What is a first order function"></a>What is a first order function</h3><p>First-order function is a function that doesn’t accept another function as an argument and doesn’t return a function as its return value.</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> firstOrder = <span class="function">() =&gt;</span> <span class="built_in">console</span>.log (<span class="string">&#x27;I am a first order function!&#x27;</span>);</span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-a-higher-order-function"><a href="#What-is-a-higher-order-function" class="headerlink" title="What is a higher order function"></a>What is a higher order function</h3><p>Higher-order function is a function that accepts another function as an argument or returns a function as a return value.</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> firstOrderFunc = <span class="function">() =&gt;</span> <span class="built_in">console</span>.log (<span class="string">&#x27;Hello I am a First order function&#x27;</span>);</span><br><span class="line"><span class="keyword">const</span> higherOrder = <span class="function"><span class="params">ReturnFirstOrderFunc</span> =&gt;</span> ReturnFirstOrderFunc ();</span><br><span class="line">higherOrder (firstOrderFunc);</span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-a-unary-function"><a href="#What-is-a-unary-function" class="headerlink" title="What is a unary function"></a>What is a unary function</h3><p>Unary function (i.e. monadic) is a function that accepts exactly one argument. Let us take an example of unary function. It stands for a single argument accepted by a function.</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> unaryFunction = <span class="function"><span class="params">a</span> =&gt;</span> <span class="built_in">console</span>.log (a + <span class="number">10</span>); <span class="comment">// Add 10 to the given argument and display the value</span></span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-the-currying-function"><a href="#What-is-the-currying-function" class="headerlink" title="What is the currying function"></a>What is the currying function</h3><p>Currying is the process of taking a function with multiple arguments and turning it into a sequence of functions each with only a single argument. Currying is named after a mathematician Haskell Curry. By applying currying, a n-ary function turns it into a unary function. Let’s take an example of n-ary function and how it turns into a currying function</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> multiArgFunction = <span class="function">(<span class="params">a, b, c</span>) =&gt;</span> a + b + c;</span><br><span class="line"><span class="keyword">const</span> curryUnaryFunction = <span class="function"><span class="params">a</span> =&gt;</span> <span class="function"><span class="params">b</span> =&gt;</span> <span class="function"><span class="params">c</span> =&gt;</span> a + b + c;</span><br><span class="line">curryUnaryFunction (<span class="number">1</span>); <span class="comment">// returns a function: b =&gt; c =&gt;  1 + b + c</span></span><br><span class="line">curryUnaryFunction (<span class="number">1</span>) (<span class="number">2</span>); <span class="comment">// returns a function: c =&gt; 3 + c</span></span><br><span class="line">curryUnaryFunction (<span class="number">1</span>) (<span class="number">2</span>) (<span class="number">3</span>); <span class="comment">// returns the number 6</span></span><br></pre></td></tr></table></figure><p>Curried functions are great to improve code reusability and functional composition.</p><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-a-pure-function"><a href="#What-is-a-pure-function" class="headerlink" title="What is a pure function"></a>What is a pure function</h3><p>A <strong>Pure function</strong> is a function where the return value is only determined by its arguments without any side effects. i.e, If you call a function with the same arguments ‘n’ number of times and ‘n’ number of places in the application then it will always return the same value. Let’s take an example to see the difference between pure and impure functions,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//Impure</span></span><br><span class="line"><span class="keyword">let</span> numberArray = [];</span><br><span class="line"><span class="keyword">const</span> impureAddNumber = <span class="function"><span class="params">number</span> =&gt;</span> numberArray.push (number);</span><br><span class="line"><span class="comment">//Pure</span></span><br><span class="line"><span class="keyword">const</span> pureAddNumber = <span class="function"><span class="params">number</span> =&gt;</span> <span class="function"><span class="params">argNumberArray</span> =&gt;</span></span><br><span class="line">  argNumberArray.concat ([number]);</span><br><span class="line"></span><br><span class="line"><span class="comment">//Display the results</span></span><br><span class="line"><span class="built_in">console</span>.log (impureAddNumber (<span class="number">6</span>)); <span class="comment">// returns 1</span></span><br><span class="line"><span class="built_in">console</span>.log (numberArray); <span class="comment">// returns [6]</span></span><br><span class="line"><span class="built_in">console</span>.log (pureAddNumber (<span class="number">7</span>) (numberArray)); <span class="comment">// returns [6, 7]</span></span><br><span class="line"><span class="built_in">console</span>.log (numberArray); <span class="comment">// returns [6]</span></span><br></pre></td></tr></table></figure><p>As per above code snippets, Push function is impure itself by altering the array and returning an push number index which is independent of parameter value. Whereas Concat on the other hand takes the array and concatenates it with the other array producing a whole new array without side effects. Also, the return value is a concatenation of the previous array.<br>Remember that Pure functions are important as they simplify unit testing without any side effects and no need for dependency injection. They also avoid tight coupling and make it harder to break your application by not having any side effects. These principles are coming together with <strong>Immutability</strong> concept of ES6 by giving preference to <strong>const</strong> over <strong>let</strong> usage.</p><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-the-purpose-of-the-let-keyword"><a href="#What-is-the-purpose-of-the-let-keyword" class="headerlink" title="What is the purpose of the let keyword"></a>What is the purpose of the let keyword</h3><p>The <code>let</code> statement declares a <strong>block scope local variable</strong>. Hence the variables defined with let keyword are limited in scope to the block, statement, or expression on which it is used. Whereas variables declared with the var keyword used to define a variable globally, or locally to an entire function regardless of block scope. Let’s take an example to demonstrate the usage,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> counter = <span class="number">30</span>;</span><br><span class="line"><span class="keyword">if</span> (counter === <span class="number">30</span>) &#123;</span><br><span class="line">  <span class="keyword">let</span> counter = <span class="number">31</span>;</span><br><span class="line">  <span class="built_in">console</span>.log(counter); <span class="comment">// 31</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="built_in">console</span>.log(counter); <span class="comment">// 30 (because if block variable won&#x27;t exist here)</span></span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-the-difference-between-let-and-var"><a href="#What-is-the-difference-between-let-and-var" class="headerlink" title="What is the difference between let and var"></a>What is the difference between let and var</h3><p>You can list out the differences in a tabular format</p><table><thead><tr><th>var</th><th>let</th></tr></thead><tbody><tr><td>It is been available from the beginning of JavaScript</td><td>Introduced as part of ES6</td></tr><tr><td>It has function scope</td><td>It has block scope</td></tr><tr><td>Variables will be hoisted</td><td>Hoisted but not initialized</td></tr></tbody></table><p>Let’s take an example to see the difference,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">userDetails</span>(<span class="params">username</span>) </span>&#123;</span><br><span class="line">   <span class="keyword">if</span>(username) &#123;</span><br><span class="line">     <span class="built_in">console</span>.log(salary); <span class="comment">// undefined(due to hoisting)</span></span><br><span class="line">     <span class="built_in">console</span>.log(age); <span class="comment">// error: age is not defined</span></span><br><span class="line">     <span class="keyword">let</span> age = <span class="number">30</span>;</span><br><span class="line">     <span class="keyword">var</span> salary = <span class="number">10000</span>;</span><br><span class="line">   &#125;</span><br><span class="line">   <span class="built_in">console</span>.log(salary); <span class="comment">//10000 (accessible to due function scope)</span></span><br><span class="line">   <span class="built_in">console</span>.log(age); <span class="comment">//error: age is not defined(due to block scope)</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-the-reason-to-choose-the-name-let-as-a-keyword"><a href="#What-is-the-reason-to-choose-the-name-let-as-a-keyword" class="headerlink" title="What is the reason to choose the name let as a keyword"></a>What is the reason to choose the name let as a keyword</h3><p>Let is a mathematical statement that was adopted by early programming languages like Scheme and Basic. It has been borrowed from dozens of other languages that use let already as a traditional keyword as close to var as possible.</p><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="How-do-you-redeclare-variables-in-switch-block-without-an-error"><a href="#How-do-you-redeclare-variables-in-switch-block-without-an-error" class="headerlink" title="How do you redeclare variables in switch block without an error"></a>How do you redeclare variables in switch block without an error</h3><p>If you try to redeclare variables in a <code>switch block</code> then it will cause errors because there is only one block. For example, the below code block throws a syntax error as below,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> counter = <span class="number">1</span>;</span><br><span class="line"><span class="keyword">switch</span>(x) &#123;</span><br><span class="line">  <span class="keyword">case</span> <span class="number">0</span>:</span><br><span class="line">    <span class="keyword">let</span> name;</span><br><span class="line">    <span class="keyword">break</span>;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">case</span> <span class="number">1</span>:</span><br><span class="line">    <span class="keyword">let</span> name; <span class="comment">// SyntaxError for redeclaration.</span></span><br><span class="line">    <span class="keyword">break</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>To avoid this error, you can create a nested block inside a case clause and create a new block scoped lexical environment.</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> counter = <span class="number">1</span>;</span><br><span class="line">    <span class="keyword">switch</span>(x) &#123;</span><br><span class="line">      <span class="keyword">case</span> <span class="number">0</span>: &#123;</span><br><span class="line">        <span class="keyword">let</span> name;</span><br><span class="line">        <span class="keyword">break</span>;</span><br><span class="line">      &#125;</span><br><span class="line">      <span class="keyword">case</span> <span class="number">1</span>: &#123;</span><br><span class="line">        <span class="keyword">let</span> name; <span class="comment">// No SyntaxError for redeclaration.</span></span><br><span class="line">        <span class="keyword">break</span>;</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-the-Temporal-Dead-Zone"><a href="#What-is-the-Temporal-Dead-Zone" class="headerlink" title="What is the Temporal Dead Zone"></a>What is the Temporal Dead Zone</h3><p>The Temporal Dead Zone is a behavior in JavaScript that occurs when declaring a variable with the let and const keywords, but not with var. In ECMAScript 6, accessing a let or const variable before its declaration (within its scope) causes a ReferenceError. The time span when that happens, between the creation of a variable’s binding and its declaration, is called the temporal dead zone. Let’s see this behavior with an example,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">somemethod</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">  <span class="built_in">console</span>.log(counter1); <span class="comment">// undefined</span></span><br><span class="line">  <span class="built_in">console</span>.log(counter2); <span class="comment">// ReferenceError</span></span><br><span class="line">  <span class="keyword">var</span> counter1 = <span class="number">1</span>;</span><br><span class="line">  <span class="keyword">let</span> counter2 = <span class="number">2</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-IIFE-Immediately-Invoked-Function-Expression"><a href="#What-is-IIFE-Immediately-Invoked-Function-Expression" class="headerlink" title="What is IIFE(Immediately Invoked Function Expression)"></a>What is IIFE(Immediately Invoked Function Expression)</h3><p>IIFE (Immediately Invoked Function Expression) is a JavaScript function that runs as soon as it is defined. The signature of it would be as below,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">(<span class="function"><span class="keyword">function</span> (<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">      <span class="comment">// logic here</span></span><br><span class="line">    &#125;</span><br><span class="line"> )</span><br><span class="line">();</span><br></pre></td></tr></table></figure><p>The primary reason to use an IIFE is to obtain data privacy because any variables declared within the IIFE cannot be accessed by the outside world. i.e, If you try to access variables with IIFE then it throws an error as below,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">(<span class="function"><span class="keyword">function</span> (<span class="params"></span>)</span></span><br><span class="line"><span class="function">        </span>&#123;</span><br><span class="line">          <span class="keyword">var</span> message = <span class="string">&quot;IIFE&quot;</span>;</span><br><span class="line">          <span class="built_in">console</span>.log(message);</span><br><span class="line">        &#125;</span><br><span class="line"> )</span><br><span class="line">();</span><br><span class="line"><span class="built_in">console</span>.log(message); <span class="comment">//Error: message is not defined</span></span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-the-benefit-of-using-modules"><a href="#What-is-the-benefit-of-using-modules" class="headerlink" title="What is the benefit of using modules"></a>What is the benefit of using modules</h3><p>There are a lot of benefits to using modules in favour of a sprawling. Some of the benefits are,</p><ol><li>Maintainability</li><li>Reusability</li><li>Namespacing</li></ol><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-memoization"><a href="#What-is-memoization" class="headerlink" title="What is memoization"></a>What is memoization</h3><p>Memoization is a programming technique which attempts to increase a function’s performance by caching its previously computed results. Each time a memoized function is called, its parameters are used to index the cache. If the data is present, then it can be returned, without executing the entire function. Otherwise the function is executed and then the result is added to the cache.<br>Let’s take an example of adding function with memoization,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> memoizAddition = <span class="function">() =&gt;</span> &#123;</span><br><span class="line">  <span class="keyword">let</span> cache = &#123;&#125;;</span><br><span class="line"> <span class="keyword">return</span> <span class="function">(<span class="params">value</span>) =&gt;</span> &#123;</span><br><span class="line">  <span class="keyword">if</span> (value <span class="keyword">in</span> cache) &#123;</span><br><span class="line">   <span class="built_in">console</span>.log(<span class="string">&#x27;Fetching from cache&#x27;</span>);</span><br><span class="line">   <span class="keyword">return</span> cache[value]; <span class="comment">// Here, cache.value cannot be used as property name starts with the number which is not a valid JavaScript  identifier. Hence, can only be accessed using the square bracket notation.</span></span><br><span class="line">  &#125;</span><br><span class="line">  <span class="keyword">else</span> &#123;</span><br><span class="line">   <span class="built_in">console</span>.log(<span class="string">&#x27;Calculating result&#x27;</span>);</span><br><span class="line">   <span class="keyword">let</span> result = value + <span class="number">20</span>;</span><br><span class="line">   cache[value] = result;</span><br><span class="line">   <span class="keyword">return</span> result;</span><br><span class="line">  &#125;</span><br><span class="line"> &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// returned function from memoizAddition</span></span><br><span class="line"><span class="keyword">const</span> addition = memoizAddition();</span><br><span class="line"><span class="built_in">console</span>.log(addition(<span class="number">20</span>)); <span class="comment">//output: 40 calculated</span></span><br><span class="line"><span class="built_in">console</span>.log(addition(<span class="number">20</span>)); <span class="comment">//output: 40 cached</span></span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-Hoisting"><a href="#What-is-Hoisting" class="headerlink" title="What is Hoisting"></a>What is Hoisting</h3><p>Hoisting is a JavaScript mechanism where variables and function declarations are moved to the top of their scope before code execution. Remember that JavaScript only hoists declarations, not initialisation.<br>Let’s take a simple example of variable hoisting,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">console</span>.log(message); <span class="comment">//output : undefined</span></span><br><span class="line"><span class="keyword">var</span> message = <span class="string">&#x27;The variable Has been hoisted&#x27;</span>;</span><br></pre></td></tr></table></figure><p>The above code looks like as below to the interpreter,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> message;</span><br><span class="line"><span class="built_in">console</span>.log(message);</span><br><span class="line">message = <span class="string">&#x27;The variable Has been hoisted&#x27;</span>;</span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-are-classes-in-ES6"><a href="#What-are-classes-in-ES6" class="headerlink" title="What are classes in ES6"></a>What are classes in ES6</h3><p>In ES6, Javascript classes are primarily syntactic sugar over JavaScript’s existing prototype-based inheritance.<br>For example, the prototype based inheritance written in function expression as below,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">Bike</span>(<span class="params">model,color</span>) </span>&#123;</span><br><span class="line">    <span class="built_in">this</span>.model = model;</span><br><span class="line">    <span class="built_in">this</span>.color = color;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">Bike.prototype.getDetails = <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="built_in">this</span>.model + <span class="string">&#x27; bike has&#x27;</span> + <span class="built_in">this</span>.color + <span class="string">&#x27; color&#x27;</span>;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure><p>Whereas ES6 classes can be defined as an alternative</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Bike</span></span>&#123;</span><br><span class="line">  <span class="function"><span class="title">constructor</span>(<span class="params">color, model</span>)</span> &#123;</span><br><span class="line">    <span class="built_in">this</span>.color= color;</span><br><span class="line">    <span class="built_in">this</span>.model= model;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="function"><span class="title">getDetails</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="built_in">this</span>.model + <span class="string">&#x27; bike has&#x27;</span> + <span class="built_in">this</span>.color + <span class="string">&#x27; color&#x27;</span>;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-are-closures"><a href="#What-are-closures" class="headerlink" title="What are closures"></a>What are closures</h3><p>A closure is the combination of a function and the lexical environment within which that function was declared. i.e, It is an inner function that has access to the outer or enclosing function’s variables. The closure has three scope chains</p><ol><li>Own scope where variables defined between its curly brackets</li><li>Outer function’s variables</li><li>Global variables<br>Let’s take an example of closure concept,</li></ol><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">Welcome</span>(<span class="params">name</span>)</span>&#123;</span><br><span class="line">  <span class="keyword">var</span> greetingInfo = <span class="function"><span class="keyword">function</span>(<span class="params">message</span>)</span>&#123;</span><br><span class="line">   <span class="built_in">console</span>.log(message+<span class="string">&#x27; &#x27;</span>+name);</span><br><span class="line">  &#125;</span><br><span class="line"><span class="keyword">return</span> greetingInfo;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">var</span> myFunction = Welcome(<span class="string">&#x27;John&#x27;</span>);</span><br><span class="line">myFunction(<span class="string">&#x27;Welcome &#x27;</span>); <span class="comment">//Output: Welcome John</span></span><br><span class="line">myFunction(<span class="string">&#x27;Hello Mr.&#x27;</span>); <span class="comment">//output: Hello Mr.John</span></span><br></pre></td></tr></table></figure><p>As per the above code, the inner function(greetingInfo) has access to the variables in the outer function scope(Welcome) even after the outer function has returned.</p><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-are-modules"><a href="#What-are-modules" class="headerlink" title="What are modules"></a>What are modules</h3><p>Modules refer to small units of independent, reusable code and also act as the foundation of many JavaScript design patterns. Most of the JavaScript modules export an object literal, a function, or a constructor</p><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="Why-do-you-need-modules"><a href="#Why-do-you-need-modules" class="headerlink" title="Why do you need modules"></a>Why do you need modules</h3><p>Below are the list of benefits using modules in javascript ecosystem</p><ol><li>Maintainability</li><li>Reusability</li><li>Namespacing</li></ol><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-scope-in-javascript"><a href="#What-is-scope-in-javascript" class="headerlink" title="What is scope in javascript"></a>What is scope in javascript</h3><p>Scope is the accessibility of variables, functions, and objects in some particular part of your code during runtime. In other words, scope determines the visibility of variables and other resources in areas of your code.</p><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-a-service-worker"><a href="#What-is-a-service-worker" class="headerlink" title="What is a service worker"></a>What is a service worker</h3><p>A Service worker is basically a script (JavaScript file) that runs in the background, separate from a web page and provides features that don’t need a web page or user interaction. Some of the major features of service workers are Rich offline experiences(offline first web application development), periodic background syncs, push notifications, intercept and handle network requests and programmatically managing a cache of responses.</p><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="How-do-you-manipulate-DOM-using-a-service-worker"><a href="#How-do-you-manipulate-DOM-using-a-service-worker" class="headerlink" title="How do you manipulate DOM using a service worker"></a>How do you manipulate DOM using a service worker</h3><p>Service worker can’t access the DOM directly. But it can communicate with the pages it controls by responding to messages sent via the <code>postMessage</code> interface, and those pages can manipulate the DOM.</p><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="How-do-you-reuse-information-across-service-worker-restarts"><a href="#How-do-you-reuse-information-across-service-worker-restarts" class="headerlink" title="How do you reuse information across service worker restarts"></a>How do you reuse information across service worker restarts</h3><p>The problem with service worker is that it gets terminated when not in use, and restarted when it’s next needed, so you cannot rely on global state within a service worker’s <code>onfetch</code> and <code>onmessage</code> handlers. In this case, service workers will have access to IndexedDB API in order to persist and reuse across restarts.</p><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-IndexedDB"><a href="#What-is-IndexedDB" class="headerlink" title="What is IndexedDB"></a>What is IndexedDB</h3><p>IndexedDB is a low-level API for client-side storage of larger amounts of structured data, including files/blobs. This API uses indexes to enable high-performance searches of this data.</p><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-web-storage"><a href="#What-is-web-storage" class="headerlink" title="What is web storage"></a>What is web storage</h3><p>Web storage is an API that provides a mechanism by which browsers can store key/value pairs locally within the user’s browser, in a much more intuitive fashion than using cookies. The web storage provides two mechanisms for storing data on the client.</p><ol><li><strong>Local storage:</strong> It stores data for current origin with no expiration date.</li><li><strong>Session storage:</strong> It stores data for one session and the data is lost when the browser tab is closed.</li></ol><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-a-post-message"><a href="#What-is-a-post-message" class="headerlink" title="What is a post message"></a>What is a post message</h3><p>Post message is a method that enables cross-origin communication between Window objects.(i.e, between a page and a pop-up that it spawned, or between a page and an iframe embedded within it). Generally, scripts on different pages are allowed to access each other if and only if the pages follow same-origin policy(i.e, pages share the same protocol, port number, and host).</p><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-a-Cookie"><a href="#What-is-a-Cookie" class="headerlink" title="What is a Cookie"></a>What is a Cookie</h3><p>A cookie is a piece of data that is stored on your computer to be accessed by your browser. Cookies are saved as key/value pairs.<br>For example, you can create a cookie named username as below,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">document</span>.cookie = <span class="string">&quot;username=John&quot;</span>;</span><br></pre></td></tr></table></figure><p><img src="/images/cookie.png" alt="Screenshot"></p><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="Why-do-you-need-a-Cookie"><a href="#Why-do-you-need-a-Cookie" class="headerlink" title="Why do you need a Cookie"></a>Why do you need a Cookie</h3><p>Cookies are used to remember information about the user profile(such as username). It basically involves two steps,</p><ol><li>When a user visits a web page, the user profile can be stored in a cookie.</li><li>Next time the user visits the page, the cookie remembers the user profile.</li></ol><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-are-the-options-in-a-cookie"><a href="#What-are-the-options-in-a-cookie" class="headerlink" title="What are the options in a cookie"></a>What are the options in a cookie</h3><p>There are few below options available for a cookie,</p><ol><li>By default, the cookie is deleted when the browser is closed but you can change this behavior by setting expiry date (in UTC time).</li></ol><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">document</span>.cookie = <span class="string">&quot;username=John; expires=Sat, 8 Jun 2019 12:00:00 UTC&quot;</span>;</span><br></pre></td></tr></table></figure><ol><li>By default, the cookie belongs to a current page. But you can tell the browser what path the cookie belongs to using a path parameter.</li></ol><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">document</span>.cookie = <span class="string">&quot;username=John; path=/services&quot;</span>;</span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="How-do-you-delete-a-cookie"><a href="#How-do-you-delete-a-cookie" class="headerlink" title="How do you delete a cookie"></a>How do you delete a cookie</h3><p>You can delete a cookie by setting the expiry date as a passed date. You don’t need to specify a cookie value in this case.<br>For example, you can delete a username cookie in the current page as below.</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">document</span>.cookie = <span class="string">&quot;username=; expires=Fri, 07 Jun 2019 00:00:00 UTC; path=/;&quot;</span>;</span><br></pre></td></tr></table></figure><p><strong>Note:</strong> You should define the cookie path option to ensure that you delete the right cookie. Some browsers doesn’t allow to delete a cookie unless you specify a path parameter.</p><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-are-the-differences-between-cookie-local-storage-and-session-storage"><a href="#What-are-the-differences-between-cookie-local-storage-and-session-storage" class="headerlink" title="What are the differences between cookie, local storage and session storage"></a>What are the differences between cookie, local storage and session storage</h3><p>Below are some of the differences between cookie, local storage and session storage,</p><table><thead><tr><th>Feature</th><th>Cookie</th><th>Local storage</th><th>Session storage</th></tr></thead><tbody><tr><td>Accessed on client or server side</td><td>Both server-side &amp; client-side</td><td>client-side only</td><td>client-side only</td></tr><tr><td>Lifetime</td><td>As configured using Expires option</td><td>until deleted</td><td>until tab is closed</td></tr><tr><td>SSL support</td><td>Supported</td><td>Not supported</td><td>Not supported</td></tr><tr><td>Maximum data size</td><td>4KB</td><td>5 MB</td><td>5MB</td></tr></tbody></table><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-the-main-difference-between-localStorage-and-sessionStorage"><a href="#What-is-the-main-difference-between-localStorage-and-sessionStorage" class="headerlink" title="What is the main difference between localStorage and sessionStorage"></a>What is the main difference between localStorage and sessionStorage</h3><p>LocalStorage is the same as SessionStorage but it persists the data even when the browser is closed and reopened(i.e it has no expiration time) whereas in sessionStorage data gets cleared when the page session ends.</p><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="How-do-you-access-web-storage"><a href="#How-do-you-access-web-storage" class="headerlink" title="How do you access web storage"></a>How do you access web storage</h3><p>The Window object implements the <code>WindowLocalStorage</code> and <code>WindowSessionStorage</code> objects which has <code>localStorage</code>(window.localStorage) and <code>sessionStorage</code>(window.sessionStorage) properties respectively. These properties create an instance of the Storage object, through which data items can be set, retrieved and removed for a specific domain and storage type (session or local).<br>For example, you can read and write on local storage objects as below</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">localStorage</span>.setItem(<span class="string">&#x27;logo&#x27;</span>, <span class="built_in">document</span>.getElementById(<span class="string">&#x27;logo&#x27;</span>).value);</span><br><span class="line"><span class="built_in">localStorage</span>.getItem(<span class="string">&#x27;logo&#x27;</span>);</span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-are-the-methods-available-on-session-storage"><a href="#What-are-the-methods-available-on-session-storage" class="headerlink" title="What are the methods available on session storage"></a>What are the methods available on session storage</h3><p>The session storage provided methods for reading, writing and clearing the session data</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// Save data to sessionStorage</span></span><br><span class="line">sessionStorage.setItem(<span class="string">&#x27;key&#x27;</span>, <span class="string">&#x27;value&#x27;</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">// Get saved data from sessionStorage</span></span><br><span class="line"><span class="keyword">let</span> data = sessionStorage.getItem(<span class="string">&#x27;key&#x27;</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">// Remove saved data from sessionStorage</span></span><br><span class="line">sessionStorage.removeItem(<span class="string">&#x27;key&#x27;</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">// Remove all saved data from sessionStorage</span></span><br><span class="line">sessionStorage.clear();</span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-a-storage-event-and-its-event-handler"><a href="#What-is-a-storage-event-and-its-event-handler" class="headerlink" title="What is a storage event and its event handler"></a>What is a storage event and its event handler</h3><p>The StorageEvent is an event that fires when a storage area has been changed in the context of another document. Whereas onstorage property is an EventHandler for processing storage events.<br>The syntax would be as below</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">window</span>.onstorage = functionRef;</span><br></pre></td></tr></table></figure><p>Let’s take the example usage of onstorage event handler which logs the storage key and it’s values</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">window</span>.onstorage = <span class="function"><span class="keyword">function</span>(<span class="params">e</span>) </span>&#123;</span><br><span class="line">  <span class="built_in">console</span>.log(<span class="string">&#x27;The &#x27;</span> + e.key +</span><br><span class="line">    <span class="string">&#x27; key has been changed from &#x27;</span> + e.oldValue +</span><br><span class="line">    <span class="string">&#x27; to &#x27;</span> + e.newValue + <span class="string">&#x27;.&#x27;</span>);</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="Why-do-you-need-web-storage"><a href="#Why-do-you-need-web-storage" class="headerlink" title="Why do you need web storage"></a>Why do you need web storage</h3><p>Web storage is more secure, and large amounts of data can be stored locally, without affecting website performance. Also, the information is never transferred to the server. Hence this is a more recommended approach than Cookies.</p><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="How-do-you-check-web-storage-browser-support"><a href="#How-do-you-check-web-storage-browser-support" class="headerlink" title="How do you check web storage browser support"></a>How do you check web storage browser support</h3><p>You need to check browser support for localStorage and sessionStorage before using web storage,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">if</span> (<span class="keyword">typeof</span>(Storage) !== <span class="string">&quot;undefined&quot;</span>) &#123;</span><br><span class="line">  <span class="comment">// Code for localStorage/sessionStorage.</span></span><br><span class="line">&#125; <span class="keyword">else</span> &#123;</span><br><span class="line">  <span class="comment">// Sorry! No Web Storage support..</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="How-do-you-check-web-workers-browser-support"><a href="#How-do-you-check-web-workers-browser-support" class="headerlink" title="How do you check web workers browser support"></a>How do you check web workers browser support</h3><p>You need to check browser support for web workers before using it</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">if</span> (<span class="keyword">typeof</span>(Worker) !== <span class="string">&quot;undefined&quot;</span>) &#123;</span><br><span class="line">  <span class="comment">// code for Web worker support.</span></span><br><span class="line">&#125; <span class="keyword">else</span> &#123;</span><br><span class="line">  <span class="comment">// Sorry! No Web Worker support..</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="Give-an-example-of-a-web-worker"><a href="#Give-an-example-of-a-web-worker" class="headerlink" title="Give an example of a web worker"></a>Give an example of a web worker</h3><p>You need to follow below steps to start using web workers for counting example</p><ol><li>Create a Web Worker File: You need to write a script to increment the count value. Let’s name it as counter.js</li></ol><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> i = <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">timedCount</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">  i = i + <span class="number">1</span>;</span><br><span class="line">  postMessage(i);</span><br><span class="line">  <span class="built_in">setTimeout</span>(<span class="string">&quot;timedCount()&quot;</span>,<span class="number">500</span>);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">timedCount();</span><br></pre></td></tr></table></figure><p>Here postMessage() method is used to post a message back to the HTML page</p><ol><li>Create a Web Worker Object: You can create a web worker object by checking for browser support. Let’s name this file as web_worker_example.js</li></ol><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">if</span> (<span class="keyword">typeof</span>(w) == <span class="string">&quot;undefined&quot;</span>) &#123;</span><br><span class="line">  w = <span class="keyword">new</span> Worker(<span class="string">&quot;counter.js&quot;</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>and we can receive messages from web worker</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">w.onmessage = <span class="function"><span class="keyword">function</span>(<span class="params">event</span>)</span>&#123;</span><br><span class="line">  <span class="built_in">document</span>.getElementById(<span class="string">&quot;message&quot;</span>).innerHTML = event.data;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure><ol><li>Terminate a Web Worker:<br>Web workers will continue to listen for messages (even after the external script is finished) until it is terminated. You can use the terminate() method to terminate listening to the messages.</li></ol><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">w.terminate();</span><br></pre></td></tr></table></figure><ol><li>Reuse the Web Worker: If you set the worker variable to undefined you can reuse the code</li></ol><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">w = <span class="literal">undefined</span>;</span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-are-the-restrictions-of-web-workers-on-DOM"><a href="#What-are-the-restrictions-of-web-workers-on-DOM" class="headerlink" title="What are the restrictions of web workers on DOM"></a>What are the restrictions of web workers on DOM</h3><p>WebWorkers don’t have access to below javascript objects since they are defined in an external files</p><ol><li>Window object</li><li>Document object</li><li>Parent object</li></ol><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-a-promise"><a href="#What-is-a-promise" class="headerlink" title="What is a promise"></a>What is a promise</h3><p>A promise is an object that may produce a single value some time in the future with either a resolved value or a reason that it’s not resolved(for example, network error). It will be in one of the 3 possible states: fulfilled, rejected, or pending.</p><p>The syntax of Promise creation looks like below,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> promise = <span class="keyword">new</span> <span class="built_in">Promise</span>(<span class="function"><span class="keyword">function</span>(<span class="params">resolve, reject</span>) </span>&#123;</span><br><span class="line">  <span class="comment">// promise description</span></span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure><p>The usage of a promise would be as below,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> promise = <span class="keyword">new</span> <span class="built_in">Promise</span>(<span class="function"><span class="params">resolve</span> =&gt;</span> &#123;</span><br><span class="line">  <span class="built_in">setTimeout</span>(<span class="function">() =&gt;</span> &#123;</span><br><span class="line">    resolve(<span class="string">&quot;I&#x27;m a Promise!&quot;</span>);</span><br><span class="line">  &#125;, <span class="number">5000</span>);</span><br><span class="line">&#125;, <span class="function"><span class="params">reject</span> =&gt;</span> &#123;</span><br><span class="line"></span><br><span class="line">&#125;);</span><br><span class="line"></span><br><span class="line">promise.then(<span class="function"><span class="params">value</span> =&gt;</span> <span class="built_in">console</span>.log(value));</span><br></pre></td></tr></table></figure><p>The action flow of a promise will be as below,</p><p><img src="/images/promises.png" alt="Screenshot"></p><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="Why-do-you-need-a-promise"><a href="#Why-do-you-need-a-promise" class="headerlink" title="Why do you need a promise"></a>Why do you need a promise</h3><p>Promises are used to handle asynchronous operations. They provide an alternative approach for callbacks by reducing the callback hell and writing the cleaner code.</p><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-are-the-three-states-of-promise"><a href="#What-are-the-three-states-of-promise" class="headerlink" title="What are the three states of promise"></a>What are the three states of promise</h3><p>Promises have three states:</p><ol><li><strong>Pending:</strong> This is an initial state of the Promise before an operation begins</li><li><strong>Fulfilled:</strong> This state indicates that the specified operation was completed.</li><li><strong>Rejected:</strong> This state indicates that the operation did not complete. In this case an error value will be thrown.</li></ol><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-a-callback-function"><a href="#What-is-a-callback-function" class="headerlink" title="What is a callback function"></a>What is a callback function</h3><p>A callback function is a function passed into another function as an argument. This function is invoked inside the outer function to complete an action.<br>Let’s take a simple example of how to use callback function</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">callbackFunction</span>(<span class="params">name</span>) </span>&#123;</span><br><span class="line">  <span class="built_in">console</span>.log(<span class="string">&#x27;Hello &#x27;</span> + name);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">outerFunction</span>(<span class="params">callback</span>) </span>&#123;</span><br><span class="line">  <span class="keyword">let</span> name = prompt(<span class="string">&#x27;Please enter your name.&#x27;</span>);</span><br><span class="line">  callback(name);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">outerFunction(callbackFunction);</span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="Why-do-we-need-callbacks"><a href="#Why-do-we-need-callbacks" class="headerlink" title="Why do we need callbacks"></a>Why do we need callbacks</h3><p>The callbacks are needed because javascript is an event driven language. That means instead of waiting for a response javascript will keep executing while listening for other events.<br>Let’s take an example with the first function invoking an API call(simulated by setTimeout) and the next function which logs the message.</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">firstFunction</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">  <span class="comment">// Simulate a code delay</span></span><br><span class="line">  <span class="built_in">setTimeout</span>( <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">&#x27;First function called&#x27;</span>);</span><br><span class="line">  &#125;, <span class="number">1000</span> );</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">secondFunction</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">  <span class="built_in">console</span>.log(<span class="string">&#x27;Second function called&#x27;</span>);</span><br><span class="line">&#125;</span><br><span class="line">firstFunction();</span><br><span class="line">secondFunction();</span><br><span class="line"></span><br><span class="line">Output</span><br><span class="line"><span class="comment">// Second function called</span></span><br><span class="line"><span class="comment">// First function called</span></span><br></pre></td></tr></table></figure><p>As observed from the output, javascript didn’t wait for the response of the first function and the remaining code block got executed. So callbacks are used in a way to make sure that certain code doesn’t execute until the other code finishes execution.</p><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-a-callback-hell"><a href="#What-is-a-callback-hell" class="headerlink" title="What is a callback hell"></a>What is a callback hell</h3><p>Callback Hell is an anti-pattern with multiple nested callbacks which makes code hard to read and debug when dealing with asynchronous logic. The callback hell looks like below,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">async1(<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    async2(<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        async3(<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">            async4(<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">                ....</span><br><span class="line">            &#125;);</span><br><span class="line">        &#125;);</span><br><span class="line">    &#125;);</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-are-server-sent-events"><a href="#What-are-server-sent-events" class="headerlink" title="What are server-sent events"></a>What are server-sent events</h3><p>Server-sent events (SSE) is a server push technology enabling a browser to receive automatic updates from a server via HTTP connection without resorting to polling. These are a one way communications channel - events flow from server to client only. This has been used in Facebook/Twitter updates, stock price updates, news feeds etc.</p><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="How-do-you-receive-server-sent-event-notifications"><a href="#How-do-you-receive-server-sent-event-notifications" class="headerlink" title="How do you receive server-sent event notifications"></a>How do you receive server-sent event notifications</h3><p>The EventSource object is used to receive server-sent event notifications. For example, you can receive messages from server as below,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">if</span>(<span class="keyword">typeof</span>(EventSource) !== <span class="string">&quot;undefined&quot;</span>) &#123;</span><br><span class="line">  <span class="keyword">var</span> source = <span class="keyword">new</span> EventSource(<span class="string">&quot;sse_generator.js&quot;</span>);</span><br><span class="line">  source.onmessage = <span class="function"><span class="keyword">function</span>(<span class="params">event</span>) </span>&#123;</span><br><span class="line">    <span class="built_in">document</span>.getElementById(<span class="string">&quot;output&quot;</span>).innerHTML += event.data + <span class="string">&quot;&lt;br&gt;&quot;</span>;</span><br><span class="line">  &#125;;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="How-do-you-check-browser-support-for-server-sent-events"><a href="#How-do-you-check-browser-support-for-server-sent-events" class="headerlink" title="How do you check browser support for server-sent events"></a>How do you check browser support for server-sent events</h3><p>You can perform browser support for server-sent events before using it as below,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">if</span>(<span class="keyword">typeof</span>(EventSource) !== <span class="string">&quot;undefined&quot;</span>) &#123;</span><br><span class="line">  <span class="comment">// Server-sent events supported. Let&#x27;s have some code here!</span></span><br><span class="line">&#125; <span class="keyword">else</span> &#123;</span><br><span class="line">  <span class="comment">// No server-sent events supported</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-are-the-events-available-for-server-sent-events"><a href="#What-are-the-events-available-for-server-sent-events" class="headerlink" title="What are the events available for server sent events"></a>What are the events available for server sent events</h3><p>Below are the list of events available for server sent events</p><table><thead><tr><th>Event</th><th>Description</th></tr></thead><tbody><tr><td>onopen</td><td>It is used when a connection to the server is opened</td></tr><tr><td>onmessage</td><td>This event is used when a message is received</td></tr><tr><td>onerror</td><td>It happens when an error occurs</td></tr></tbody></table><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-are-the-main-rules-of-promise"><a href="#What-are-the-main-rules-of-promise" class="headerlink" title="What are the main rules of promise"></a>What are the main rules of promise</h3><p>A promise must follow a specific set of rules,</p><ol><li>A promise is an object that supplies a standard-compliant <code>.then()</code> method</li><li>A pending promise may transition into either fulfilled or rejected state</li><li>A fulfilled or rejected promise is settled and it must not transition into any other state.</li><li>Once a promise is settled, the value must not change.</li></ol><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-callback-in-callback"><a href="#What-is-callback-in-callback" class="headerlink" title="What is callback in callback"></a>What is callback in callback</h3><p>You can nest one callback inside in another callback to execute the actions sequentially one by one. This is known as callbacks in callbacks.</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">loadScript(<span class="string">&#x27;/script1.js&#x27;</span>, <span class="function"><span class="keyword">function</span>(<span class="params">script</span>) </span>&#123;</span><br><span class="line">   <span class="built_in">console</span>.log(<span class="string">&#x27;first script is loaded&#x27;</span>);</span><br><span class="line"></span><br><span class="line">  loadScript(<span class="string">&#x27;/script2.js&#x27;</span>, <span class="function"><span class="keyword">function</span>(<span class="params">script</span>) </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">&#x27;second script is loaded&#x27;</span>);</span><br><span class="line"></span><br><span class="line">    loadScript(<span class="string">&#x27;/script3.js&#x27;</span>, <span class="function"><span class="keyword">function</span>(<span class="params">script</span>) </span>&#123;</span><br><span class="line"></span><br><span class="line">        <span class="built_in">console</span>.log(<span class="string">&#x27;third script is loaded&#x27;</span>);</span><br><span class="line">      <span class="comment">// after all scripts are loaded</span></span><br><span class="line">    &#125;);</span><br><span class="line"></span><br><span class="line">  &#125;)</span><br><span class="line"></span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-promise-chaining"><a href="#What-is-promise-chaining" class="headerlink" title="What is promise chaining"></a>What is promise chaining</h3><p>The process of executing a sequence of asynchronous tasks one after another using promises is known as Promise chaining. Let’s take an example of promise chaining for calculating the final result,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">new</span> <span class="built_in">Promise</span>(<span class="function"><span class="keyword">function</span>(<span class="params">resolve, reject</span>) </span>&#123;</span><br><span class="line"></span><br><span class="line">  <span class="built_in">setTimeout</span>(<span class="function">() =&gt;</span> resolve(<span class="number">1</span>), <span class="number">1000</span>);</span><br><span class="line"></span><br><span class="line">&#125;).then(<span class="function"><span class="keyword">function</span>(<span class="params">result</span>) </span>&#123;</span><br><span class="line"></span><br><span class="line">  <span class="built_in">console</span>.log(result); <span class="comment">// 1</span></span><br><span class="line">  <span class="keyword">return</span> result * <span class="number">2</span>;</span><br><span class="line"></span><br><span class="line">&#125;).then(<span class="function"><span class="keyword">function</span>(<span class="params">result</span>) </span>&#123;</span><br><span class="line"></span><br><span class="line">  <span class="built_in">console</span>.log(result); <span class="comment">// 2</span></span><br><span class="line">  <span class="keyword">return</span> result * <span class="number">3</span>;</span><br><span class="line"></span><br><span class="line">&#125;).then(<span class="function"><span class="keyword">function</span>(<span class="params">result</span>) </span>&#123;</span><br><span class="line"></span><br><span class="line">  <span class="built_in">console</span>.log(result); <span class="comment">// 6</span></span><br><span class="line">  <span class="keyword">return</span> result * <span class="number">4</span>;</span><br><span class="line"></span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure><p>In the above handlers, the result is passed to the chain of .then() handlers with the below work flow,</p><ol><li>The initial promise resolves in 1 second,</li><li>After that <code>.then</code> handler is called by logging the result(1) and then return a promise with the value of result * 2.</li><li>After that the value passed to the next <code>.then</code> handler by logging the result(2) and return a promise with result * 3.</li><li>Finally the value passed to the last <code>.then</code> handler by logging the result(6) and return a promise with result * 4.</li></ol><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-promise-all"><a href="#What-is-promise-all" class="headerlink" title="What is promise.all"></a>What is promise.all</h3><p>Promise.all is a promise that takes an array of promises as an input (an iterable), and it gets resolved when all the promises get resolved or any one of them gets rejected. For example, the syntax of promise.all method is below,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">Promise</span>.all([Promise1, Promise2, Promise3]) .then(result) =&gt; &#123;   <span class="built_in">console</span>.log(result) &#125;) .catch(<span class="function"><span class="params">error</span> =&gt;</span> <span class="built_in">console</span>.log(<span class="string">`Error in promises <span class="subst">$&#123;error&#125;</span>`</span>))</span><br></pre></td></tr></table></figure><p><strong>Note:</strong> Remember that the order of the promises(output the result) is maintained as per input order.</p><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-the-purpose-of-the-race-method-in-promise"><a href="#What-is-the-purpose-of-the-race-method-in-promise" class="headerlink" title="What is the purpose of the race method in promise"></a>What is the purpose of the race method in promise</h3><p>Promise.race() method will return the promise instance which is firstly resolved or rejected. Let’s take an example of race() method where promise2 is resolved first</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> promise1 = <span class="keyword">new</span> <span class="built_in">Promise</span>(<span class="function"><span class="keyword">function</span>(<span class="params">resolve, reject</span>) </span>&#123;</span><br><span class="line">    <span class="built_in">setTimeout</span>(resolve, <span class="number">500</span>, <span class="string">&#x27;one&#x27;</span>);</span><br><span class="line">&#125;);</span><br><span class="line"><span class="keyword">var</span> promise2 = <span class="keyword">new</span> <span class="built_in">Promise</span>(<span class="function"><span class="keyword">function</span>(<span class="params">resolve, reject</span>) </span>&#123;</span><br><span class="line">    <span class="built_in">setTimeout</span>(resolve, <span class="number">100</span>, <span class="string">&#x27;two&#x27;</span>);</span><br><span class="line">&#125;);</span><br><span class="line"></span><br><span class="line"><span class="built_in">Promise</span>.race([promise1, promise2]).then(<span class="function"><span class="keyword">function</span>(<span class="params">value</span>) </span>&#123;</span><br><span class="line">  <span class="built_in">console</span>.log(value); <span class="comment">// &quot;two&quot; // Both promises will resolve, but promise2 is faster</span></span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-a-strict-mode-in-javascript"><a href="#What-is-a-strict-mode-in-javascript" class="headerlink" title="What is a strict mode in javascript"></a>What is a strict mode in javascript</h3><p>Strict Mode is a new feature in ECMAScript 5 that allows you to place a program, or a function, in a “strict” operating context. This way it prevents certain actions from being taken and throws more exceptions. The literal expression <code>&quot;use strict&quot;;</code> instructs the browser to use the javascript code in the Strict mode.</p><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="Why-do-you-need-strict-mode"><a href="#Why-do-you-need-strict-mode" class="headerlink" title="Why do you need strict mode"></a>Why do you need strict mode</h3><p>Strict mode is useful to write “secure” JavaScript by notifying “bad syntax” into real errors. For example, it eliminates accidentally creating a global variable by throwing an error and also throws an error for assignment to a non-writable property, a getter-only property, a non-existing property, a non-existing variable, or a non-existing object.</p><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="How-do-you-declare-strict-mode"><a href="#How-do-you-declare-strict-mode" class="headerlink" title="How do you declare strict mode"></a>How do you declare strict mode</h3><p>The strict mode is declared by adding “use strict”; to the beginning of a script or a function.<br>If declared at the beginning of a script, it has global scope.</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&quot;use strict&quot;</span>;</span><br><span class="line">x = <span class="number">3.14</span>; <span class="comment">// This will cause an error because x is not declared</span></span><br></pre></td></tr></table></figure><p>and if you declare inside a function, it has local scope</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">x = <span class="number">3.14</span>;       <span class="comment">// This will not cause an error.</span></span><br><span class="line">myFunction();</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">myFunction</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line"><span class="meta">  &quot;use strict&quot;</span>;</span><br><span class="line">  y = <span class="number">3.14</span>;   <span class="comment">// This will cause an error</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-the-purpose-of-double-exclamation"><a href="#What-is-the-purpose-of-double-exclamation" class="headerlink" title="What is the purpose of double exclamation"></a>What is the purpose of double exclamation</h3><p>The double exclamation or negation(!!) ensures the resulting type is a boolean. If it was falsey (e.g. 0, null, undefined, etc.), it will be false, otherwise, true.<br>For example, you can test IE version using this expression as below,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> isIE8 = <span class="literal">false</span>;</span><br><span class="line">isIE8 = !! navigator.userAgent.match(<span class="regexp">/MSIE 8.0/</span>);</span><br><span class="line"><span class="built_in">console</span>.log(isIE8); <span class="comment">// returns true or false</span></span><br></pre></td></tr></table></figure><p>If you don’t use this expression then it returns the original value.</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">console</span>.log(navigator.userAgent.match(<span class="regexp">/MSIE 8.0/</span>));  <span class="comment">// returns either an Array or null</span></span><br></pre></td></tr></table></figure><p><strong>Note:</strong> The expression !! is not an operator, but it is just twice of ! operator.</p><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-the-purpose-of-the-delete-operator"><a href="#What-is-the-purpose-of-the-delete-operator" class="headerlink" title="What is the purpose of the delete operator"></a>What is the purpose of the delete operator</h3><p>The delete keyword is used to delete the property as well as its value.</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> user= &#123;<span class="attr">name</span>: <span class="string">&quot;John&quot;</span>, <span class="attr">age</span>:<span class="number">20</span>&#125;;</span><br><span class="line"><span class="keyword">delete</span> user.age;</span><br><span class="line"></span><br><span class="line"><span class="built_in">console</span>.log(user); <span class="comment">// &#123;name: &quot;John&quot;&#125;</span></span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-the-typeof-operator"><a href="#What-is-the-typeof-operator" class="headerlink" title="What is the typeof operator"></a>What is the typeof operator</h3><p>You can use the JavaScript typeof operator to find the type of a JavaScript variable. It returns the type of a variable or an expression.</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">typeof</span> <span class="string">&quot;John Abraham&quot;</span>     <span class="comment">// Returns &quot;string&quot;</span></span><br><span class="line"><span class="keyword">typeof</span> (<span class="number">1</span> + <span class="number">2</span>)        <span class="comment">// Returns &quot;number&quot;</span></span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-undefined-property"><a href="#What-is-undefined-property" class="headerlink" title="What is undefined property"></a>What is undefined property</h3><p>The undefined property indicates that a variable has not been assigned a value, or not declared at all. The type of undefined value is undefined too.</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> user;    <span class="comment">// Value is undefined, type is undefined</span></span><br><span class="line"><span class="built_in">console</span>.log(<span class="keyword">typeof</span>(user)) <span class="comment">//undefined</span></span><br></pre></td></tr></table></figure><p>Any variable can be emptied by setting the value to undefined.</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">user = <span class="literal">undefined</span></span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-null-value"><a href="#What-is-null-value" class="headerlink" title="What is null value"></a>What is null value</h3><p>The value null represents the intentional absence of any object value. It is one of JavaScript’s primitive values. The type of null value is object.<br>You can empty the variable by setting the value to null.</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> user = <span class="literal">null</span>;</span><br><span class="line"><span class="built_in">console</span>.log(<span class="keyword">typeof</span>(user)) <span class="comment">//object</span></span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-the-difference-between-null-and-undefined"><a href="#What-is-the-difference-between-null-and-undefined" class="headerlink" title="What is the difference between null and undefined"></a>What is the difference between null and undefined</h3><p>Below are the main differences between null and undefined,</p><table><thead><tr><th>Null</th><th>Undefined</th></tr></thead><tbody><tr><td>It is an assignment value which indicates that variable points to no object.</td><td>It is not an assignment value where a variable has been declared but has not yet been assigned a value.</td></tr><tr><td>Type of null is object</td><td>Type of undefined is undefined</td></tr><tr><td>The null value is a primitive value that represents the null, empty, or non-existent reference.</td><td>The undefined value is a primitive value used when a variable has not been assigned a value.</td></tr><tr><td>Indicates the absence of a value for a variable</td><td>Indicates absence of variable itself</td></tr><tr><td>Converted to zero (0) while performing primitive operations</td><td>Converted to NaN while performing primitive operations</td></tr></tbody></table><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-eval"><a href="#What-is-eval" class="headerlink" title="What is eval"></a>What is eval</h3><p>The eval() function evaluates JavaScript code represented as a string. The string can be a JavaScript expression, variable, statement, or sequence of statements.</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">console</span>.log(<span class="built_in">eval</span>(<span class="string">&#x27;1 + 2&#x27;</span>)); <span class="comment">//  3</span></span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-the-difference-between-window-and-document"><a href="#What-is-the-difference-between-window-and-document" class="headerlink" title="What is the difference between window and document"></a>What is the difference between window and document</h3><p>Below are the main differences between window and document,</p><table><thead><tr><th>Window</th><th>Document</th></tr></thead><tbody><tr><td>It is the root level element in any web page</td><td>It is the direct child of the window object. This is also known as Document Object Model(DOM)</td></tr><tr><td>By default window object is available implicitly in the page</td><td>You can access it via window.document or document.</td></tr><tr><td>It has methods like alert(), confirm() and properties like document, location</td><td>It provides methods like getElementById, getElementByTagName, createElement etc</td></tr></tbody></table><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="How-do-you-access-history-in-javascript"><a href="#How-do-you-access-history-in-javascript" class="headerlink" title="How do you access history in javascript"></a>How do you access history in javascript</h3><p>The window.history object contains the browser’s history. You can load previous and next URLs in the history using back() and next() methods.</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">goBack</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">  <span class="built_in">window</span>.history.back()</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">goForward</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">  <span class="built_in">window</span>.history.forward()</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p><strong>Note:</strong> You can also access history without window prefix.</p><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-are-the-javascript-data-types"><a href="#What-are-the-javascript-data-types" class="headerlink" title="What are the javascript data types"></a>What are the javascript data types</h3><p>Below are the list of javascript data types available</p><ol><li>Number</li><li>String</li><li>Boolean</li><li>Object</li><li>Undefined</li></ol><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-isNaN"><a href="#What-is-isNaN" class="headerlink" title="What is isNaN"></a>What is isNaN</h3><p>The isNaN() function is used to determine whether a value is an illegal number (Not-a-Number) or not. i.e, This function returns true if the value equates to NaN. Otherwise it returns false.</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">isNaN</span>(<span class="string">&#x27;Hello&#x27;</span>) <span class="comment">//true</span></span><br><span class="line"><span class="built_in">isNaN</span>(<span class="string">&#x27;100&#x27;</span>) <span class="comment">//false</span></span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-are-the-differences-between-undeclared-and-undefined-variables"><a href="#What-are-the-differences-between-undeclared-and-undefined-variables" class="headerlink" title="What are the differences between undeclared and undefined variables"></a>What are the differences between undeclared and undefined variables</h3><p>Below are the major differences between undeclared and undefined variables,</p><table><thead><tr><th>undeclared</th><th>undefined</th></tr></thead><tbody><tr><td>These variables do not exist in a program and are not declared</td><td>These variables declared in the program but have not assigned any value</td></tr><tr><td>If you try to read the value of an undeclared variable, then a runtime error is encountered</td><td>If you try to read the value of an undefined variable, an undefined value is returned.</td></tr></tbody></table><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-are-global-variables"><a href="#What-are-global-variables" class="headerlink" title="What are global variables"></a>What are global variables</h3><p>Global variables are those that are available throughout the length of the code without any scope. The var keyword is used to declare a local variable but if you omit it then it will become global variable</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">msg = <span class="string">&quot;Hello&quot;</span> <span class="comment">// var is missing, it becomes global variable</span></span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-are-the-problems-with-global-variables"><a href="#What-are-the-problems-with-global-variables" class="headerlink" title="What are the problems with global variables"></a>What are the problems with global variables</h3><p>The problem with global variables is the conflict of variable names of local and global scope. It is also difficult to debug and test the code that relies on global variables.</p><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-NaN-property"><a href="#What-is-NaN-property" class="headerlink" title="What is NaN property"></a>What is NaN property</h3><p>The NaN property is a global property that represents “Not-a-Number” value. i.e, It indicates that a value is not a legal number. It is very rare to use NaN in a program but it can be used as return value for few cases</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">Math</span>.sqrt(-<span class="number">1</span>)</span><br><span class="line"><span class="built_in">parseInt</span>(<span class="string">&quot;Hello&quot;</span>)</span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-the-purpose-of-isFinite-function"><a href="#What-is-the-purpose-of-isFinite-function" class="headerlink" title="What is the purpose of isFinite function"></a>What is the purpose of isFinite function</h3><p>The isFinite() function is used to determine whether a number is a finite, legal number. It returns false if the value is +infinity, -infinity, or NaN (Not-a-Number), otherwise it returns true.</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">isFinite</span>(<span class="literal">Infinity</span>);  <span class="comment">// false</span></span><br><span class="line"><span class="built_in">isFinite</span>(<span class="literal">NaN</span>);       <span class="comment">// false</span></span><br><span class="line"><span class="built_in">isFinite</span>(-<span class="literal">Infinity</span>); <span class="comment">// false</span></span><br><span class="line"></span><br><span class="line"><span class="built_in">isFinite</span>(<span class="number">100</span>);         <span class="comment">// true</span></span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-an-event-flow"><a href="#What-is-an-event-flow" class="headerlink" title="What is an event flow"></a>What is an event flow</h3><p>Event flow is the order in which event is received on the web page. When you click an element that is nested in various other elements, before your click actually reaches its destination, or target element, it must trigger the click event for each of its parent elements first, starting at the top with the global window object.<br>There are two ways of event flow</p><ol><li>Top to Bottom(Event Capturing)</li><li>Bottom to Top (Event Bubbling)</li></ol><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-event-bubbling"><a href="#What-is-event-bubbling" class="headerlink" title="What is event bubbling"></a>What is event bubbling</h3><p>Event bubbling is a type of event propagation where the event first triggers on the innermost target element, and then successively triggers on the ancestors (parents) of the target element in the same nesting hierarchy till it reaches the outermost DOM element.</p><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-event-capturing"><a href="#What-is-event-capturing" class="headerlink" title="What is event capturing"></a>What is event capturing</h3><p>Event capturing is a type of event propagation where the event is first captured by the outermost element, and then successively triggers on the descendants (children) of the target element in the same nesting hierarchy till it reaches the innermost DOM element.</p><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="How-do-you-submit-a-form-using-JavaScript"><a href="#How-do-you-submit-a-form-using-JavaScript" class="headerlink" title="How do you submit a form using JavaScript"></a>How do you submit a form using JavaScript</h3><p>You can submit a form using JavaScript use document.form[0].submit(). All the form input’s information is submitted using onsubmit event handler</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">submit</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="built_in">document</span>.form[<span class="number">0</span>].submit();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="How-do-you-find-operating-system-details"><a href="#How-do-you-find-operating-system-details" class="headerlink" title="How do you find operating system details"></a>How do you find operating system details</h3><p>The window.navigator object contains information about the visitor’s browser OS details. Some of the OS properties are available under platform property,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">console</span>.log(navigator.platform);</span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-the-difference-between-document-load-and-DOMContentLoaded-events"><a href="#What-is-the-difference-between-document-load-and-DOMContentLoaded-events" class="headerlink" title="What is the difference between document load and DOMContentLoaded events"></a>What is the difference between document load and DOMContentLoaded events</h3><p>The <code>DOMContentLoaded</code> event is fired when the initial HTML document has been completely loaded and parsed, without waiting for assets(stylesheets, images, and subframes) to finish loading. Whereas The load event is fired when the whole page has loaded, including all dependent resources(stylesheets, images).</p><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-the-difference-between-native-host-and-user-objects"><a href="#What-is-the-difference-between-native-host-and-user-objects" class="headerlink" title="What is the difference between native, host and user objects"></a>What is the difference between native, host and user objects</h3><p><code>Native objects</code> are objects that are part of the JavaScript language defined by the ECMAScript specification. For example, String, Math, RegExp, Object, Function etc core objects defined in the ECMAScript spec.<br><code>Host objects</code> are objects provided by the browser or runtime environment (Node). For example, window, XmlHttpRequest, DOM nodes etc are considered as host objects.<br><code>User objects</code> are objects defined in the javascript code. For example, User objects created for profile information.</p><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-are-the-tools-or-techniques-used-for-debugging-JavaScript-code"><a href="#What-are-the-tools-or-techniques-used-for-debugging-JavaScript-code" class="headerlink" title="What are the tools or techniques used for debugging JavaScript code"></a>What are the tools or techniques used for debugging JavaScript code</h3><p>You can use below tools or techniques for debugging javascript</p><ol><li>Chrome Devtools</li><li>debugger statement</li><li>Good old console.log statement</li></ol><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-are-the-pros-and-cons-of-promises-over-callbacks"><a href="#What-are-the-pros-and-cons-of-promises-over-callbacks" class="headerlink" title="What are the pros and cons of promises over callbacks"></a>What are the pros and cons of promises over callbacks</h3><p>Below are the list of pros and cons of promises over callbacks,</p><p><strong>Pros:</strong></p><ol><li>It avoids callback hell which is unreadable</li><li>Easy to write sequential asynchronous code with .then()</li><li>Easy to write parallel asynchronous code with Promise.all()</li><li>Solves some of the common problems of callbacks(call the callback too late, too early, many times and swallow errors/exceptions)</li></ol><p><strong>Cons:</strong></p><ol><li>It makes little complex code</li><li>You need to load a polyfill if ES6 is not supported</li></ol><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-the-difference-between-an-attribute-and-a-property"><a href="#What-is-the-difference-between-an-attribute-and-a-property" class="headerlink" title="What is the difference between an attribute and a property"></a>What is the difference between an attribute and a property</h3><p>Attributes are defined on the HTML markup whereas properties are defined on the DOM. For example, the below HTML element has 2 attributes type and value,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">&lt;input type=<span class="string">&quot;text&quot;</span> value=<span class="string">&quot;Name:&quot;</span>&gt;</span><br></pre></td></tr></table></figure><p>You can retrieve the attribute value as below,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> input = <span class="built_in">document</span>.querySelector(<span class="string">&#x27;input&#x27;</span>);</span><br><span class="line"><span class="built_in">console</span>.log(input.getAttribute(<span class="string">&#x27;value&#x27;</span>)); <span class="comment">// Good morning</span></span><br><span class="line"><span class="built_in">console</span>.log(input.value); <span class="comment">// Good morning</span></span><br></pre></td></tr></table></figure><p>And after you change the value of the text field to “Good evening”, it becomes like</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">console</span>.log(input.getAttribute(<span class="string">&#x27;value&#x27;</span>)); <span class="comment">// Good morning</span></span><br><span class="line"><span class="built_in">console</span>.log(input.value); <span class="comment">// Good evening</span></span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-same-origin-policy"><a href="#What-is-same-origin-policy" class="headerlink" title="What is same-origin policy"></a>What is same-origin policy</h3><p>The same-origin policy is a policy that prevents JavaScript from making requests across domain boundaries. An origin is defined as a combination of URI scheme, hostname, and port number. If you enable this policy then it prevents a malicious script on one page from obtaining access to sensitive data on another web page using Document Object Model(DOM).</p><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-the-purpose-of-void-0"><a href="#What-is-the-purpose-of-void-0" class="headerlink" title="What is the purpose of void 0"></a>What is the purpose of void 0</h3><p>Void(0) is used to prevent the page from refreshing. This will be helpful to eliminate the unwanted side-effect, because it will return the undefined primitive value. It is commonly used for HTML documents that use href=”JavaScript:Void(0);” within an <code>&lt;a&gt;</code> element. i.e, when you click a link, the browser loads a new page or refreshes the same page. But this behavior will be prevented using this expression.<br>For example, the below link notify the message without reloading the page</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">&lt;a href=<span class="string">&quot;JavaScript:void(0);&quot;</span> onclick=<span class="string">&quot;alert(&#x27;Well done!&#x27;)&quot;</span>&gt;Click Me!&lt;/a&gt;</span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="Is-JavaScript-a-compiled-or-interpreted-language"><a href="#Is-JavaScript-a-compiled-or-interpreted-language" class="headerlink" title="Is JavaScript a compiled or interpreted language"></a>Is JavaScript a compiled or interpreted language</h3><p>JavaScript is an interpreted language, not a compiled language. An interpreter in the browser reads over the JavaScript code, interprets each line, and runs it. Nowadays modern browsers use a technology known as Just-In-Time (JIT) compilation, which compiles JavaScript to executable bytecode just as it is about to run.</p><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="Is-JavaScript-a-case-sensitive-language"><a href="#Is-JavaScript-a-case-sensitive-language" class="headerlink" title="Is JavaScript a case-sensitive language"></a>Is JavaScript a case-sensitive language</h3><p>Yes, JavaScript is a case sensitive language. The language keywords, variables, function &amp; object names, and any other identifiers must always be typed with a consistent capitalization of letters.</p><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="Is-there-any-relation-between-Java-and-JavaScript"><a href="#Is-there-any-relation-between-Java-and-JavaScript" class="headerlink" title="Is there any relation between Java and JavaScript"></a>Is there any relation between Java and JavaScript</h3><p>No, they are entirely two different programming languages and have nothing to do with each other. But both of them are Object Oriented Programming languages and like many other languages, they follow similar syntax for basic features(if, else, for, switch, break, continue etc).</p><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-are-events"><a href="#What-are-events" class="headerlink" title="What are events"></a>What are events</h3><p>Events are “things” that happen to HTML elements. When JavaScript is used in HTML pages, JavaScript can <code>react</code> on these events. Some of the examples of HTML events are,</p><ol><li>Web page has finished loading</li><li>Input field was changed</li><li>Button was clicked</li></ol><p>Let’s describe the behavior of click event for button element,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">&lt;!doctype html&gt;</span><br><span class="line">&lt;html&gt;</span><br><span class="line"> &lt;head&gt;</span><br><span class="line">   &lt;script&gt;</span><br><span class="line">     <span class="function"><span class="keyword">function</span> <span class="title">greeting</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">       alert(<span class="string">&#x27;Hello! Good morning&#x27;</span>);</span><br><span class="line">     &#125;</span><br><span class="line">   &lt;/script&gt;</span><br><span class="line"> &lt;/head&gt;</span><br><span class="line"> &lt;body&gt;</span><br><span class="line">   &lt;button type=<span class="string">&quot;button&quot;</span> onclick=<span class="string">&quot;greeting()&quot;</span>&gt;Click me&lt;/button&gt;</span><br><span class="line"> &lt;/body&gt;</span><br><span class="line">&lt;/html&gt;</span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="Who-created-javascript"><a href="#Who-created-javascript" class="headerlink" title="Who created javascript"></a>Who created javascript</h3><p>JavaScript was created by Brendan Eich in 1995 during his time at Netscape Communications. Initially it was developed under the name <code>Mocha</code>, but later the language was officially called <code>LiveScript</code> when it first shipped in beta releases of Netscape.</p><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-the-use-of-preventDefault-method"><a href="#What-is-the-use-of-preventDefault-method" class="headerlink" title="What is the use of preventDefault method"></a>What is the use of preventDefault method</h3><p>The preventDefault() method cancels the event if it is cancelable, meaning that the default action or behaviour that belongs to the event will not occur. For example, prevent form submission when clicking on submit button and prevent opening the page URL when clicking on hyperlink are some common use cases.</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">document</span>.getElementById(<span class="string">&quot;link&quot;</span>).addEventListener(<span class="string">&quot;click&quot;</span>, <span class="function"><span class="keyword">function</span>(<span class="params">event</span>)</span>&#123;</span><br><span class="line"> event.preventDefault();</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure><p><strong>Note:</strong> Remember that not all events are cancelable.</p><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-the-use-of-stopPropagation-method"><a href="#What-is-the-use-of-stopPropagation-method" class="headerlink" title="What is the use of stopPropagation method"></a>What is the use of stopPropagation method</h3><p>The stopPropagation method is used to stop the event from bubbling up the event chain. For example, the below nested divs with stopPropagation method prevents default event propagation when clicking on nested div(Div1)</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">&lt;p&gt;Click DIV1 Element&lt;/p&gt;</span><br><span class="line">&lt;div onclick=<span class="string">&quot;secondFunc()&quot;</span>&gt;DIV <span class="number">2</span></span><br><span class="line">  &lt;div onclick=<span class="string">&quot;firstFunc(event)&quot;</span>&gt;DIV <span class="number">1</span>&lt;/div&gt;</span><br><span class="line">&lt;/div&gt;</span><br><span class="line"></span><br><span class="line">&lt;script&gt;</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">firstFunc</span>(<span class="params">event</span>) </span>&#123;</span><br><span class="line">  alert(<span class="string">&quot;DIV 1&quot;</span>);</span><br><span class="line">  event.stopPropagation();</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">secondFunc</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">  alert(<span class="string">&quot;DIV 2&quot;</span>);</span><br><span class="line">&#125;</span><br><span class="line">&lt;/script&gt;</span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-are-the-steps-involved-in-return-false-usage"><a href="#What-are-the-steps-involved-in-return-false-usage" class="headerlink" title="What are the steps involved in return false usage"></a>What are the steps involved in return false usage</h3><p>The return false statement in event handlers performs the below steps,</p><ol><li>First it stops the browser’s default action or behaviour.</li><li>It prevents the event from propagating the DOM</li><li>Stops callback execution and returns immediately when called.</li></ol><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-BOM"><a href="#What-is-BOM" class="headerlink" title="What is BOM"></a>What is BOM</h3><p>The Browser Object Model (BOM) allows JavaScript to “talk to” the browser. It consists of the objects navigator, history, screen, location and document which are children of the window. The Browser Object Model is not standardized and can change based on different browsers.</p><p><img src="/images/bom.png" alt="Screenshot"></p><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-the-use-of-setTimeout"><a href="#What-is-the-use-of-setTimeout" class="headerlink" title="What is the use of setTimeout"></a>What is the use of setTimeout</h3><p>The setTimeout() method is used to call a function or evaluate an expression after a specified number of milliseconds. For example, let’s log a message after 2 seconds using setTimeout method,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">setTimeout</span>(<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123; <span class="built_in">console</span>.log(<span class="string">&quot;Good morning&quot;</span>); &#125;, <span class="number">2000</span>);</span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-the-use-of-setInterval"><a href="#What-is-the-use-of-setInterval" class="headerlink" title="What is the use of setInterval"></a>What is the use of setInterval</h3><p>The setInterval() method is used to call a function or evaluate an expression at specified intervals (in milliseconds). For example, let’s log a message after 2 seconds using setInterval method,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">setInterval</span>(<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123; <span class="built_in">console</span>.log(<span class="string">&quot;Good morning&quot;</span>); &#125;, <span class="number">2000</span>);</span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="Why-is-JavaScript-treated-as-Single-threaded"><a href="#Why-is-JavaScript-treated-as-Single-threaded" class="headerlink" title="Why is JavaScript treated as Single threaded"></a>Why is JavaScript treated as Single threaded</h3><p>JavaScript is a single-threaded language. Because the language specification does not allow the programmer to write code so that the interpreter can run parts of it in parallel in multiple threads or processes. Whereas languages like java, go, C++ can make multi-threaded and multi-process programs.</p><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-an-event-delegation"><a href="#What-is-an-event-delegation" class="headerlink" title="What is an event delegation"></a>What is an event delegation</h3><p>Event delegation is a technique for listening to events where you delegate a parent element as the listener for all of the events that happen inside it.</p><p>For example, if you wanted to detect field changes in inside a specific form, you can use event delegation technique,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> form = <span class="built_in">document</span>.querySelector(<span class="string">&#x27;#registration-form&#x27;</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">// Listen for changes to fields inside the form</span></span><br><span class="line">form.addEventListener(<span class="string">&#x27;input&#x27;</span>, <span class="function"><span class="keyword">function</span> (<span class="params">event</span>) </span>&#123;</span><br><span class="line"></span><br><span class="line"><span class="comment">// Log the field that was changed</span></span><br><span class="line"><span class="built_in">console</span>.log(event.target);</span><br><span class="line"></span><br><span class="line">&#125;, <span class="literal">false</span>);</span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-ECMAScript"><a href="#What-is-ECMAScript" class="headerlink" title="What is ECMAScript"></a>What is ECMAScript</h3><p>ECMAScript is the scripting language that forms the basis of JavaScript. ECMAScript standardized by the ECMA International standards organization in the ECMA-262 and ECMA-402 specifications. The first edition of ECMAScript was released in 1997.</p><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-JSON"><a href="#What-is-JSON" class="headerlink" title="What is JSON"></a>What is JSON</h3><p>JSON (JavaScript Object Notation) is a lightweight format that is used for data interchanging. It is based on a subset of JavaScript language in the way objects are built in JavaScript.</p><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-are-the-syntax-rules-of-JSON"><a href="#What-are-the-syntax-rules-of-JSON" class="headerlink" title="What are the syntax rules of JSON"></a>What are the syntax rules of JSON</h3><p>Below are the list of syntax rules of JSON</p><ol><li>The data is in name/value pairs</li><li>The data is separated by commas</li><li>Curly braces hold objects</li><li>Square brackets hold arrays</li></ol><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-the-purpose-JSON-stringify"><a href="#What-is-the-purpose-JSON-stringify" class="headerlink" title="What is the purpose JSON stringify"></a>What is the purpose JSON stringify</h3><p>When sending data to a web server, the data has to be in a string format. You can achieve this by converting JSON object into a string using stringify() method.</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> userJSON = &#123;<span class="string">&#x27;name&#x27;</span>: <span class="string">&#x27;John&#x27;</span>, <span class="attr">age</span>: <span class="number">31</span>&#125;</span><br><span class="line"><span class="keyword">var</span> userString = <span class="built_in">JSON</span>.stringify(user);</span><br><span class="line"><span class="built_in">console</span>.log(userString); <span class="comment">//&quot;&#123;&quot;name&quot;:&quot;John&quot;,&quot;age&quot;:31&#125;&quot;</span></span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="How-do-you-parse-JSON-string"><a href="#How-do-you-parse-JSON-string" class="headerlink" title="How do you parse JSON string"></a>How do you parse JSON string</h3><p>When receiving the data from a web server, the data is always in a string format. But you can convert this string value to a javascript object using parse() method.</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> userString = <span class="string">&#x27;&#123;&quot;name&quot;:&quot;John&quot;,&quot;age&quot;:31&#125;&#x27;</span>;</span><br><span class="line"><span class="keyword">var</span> userJSON = <span class="built_in">JSON</span>.parse(userString);</span><br><span class="line"><span class="built_in">console</span>.log(userJSON);<span class="comment">// &#123;name: &quot;John&quot;, age: 31&#125;</span></span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="Why-do-you-need-JSON"><a href="#Why-do-you-need-JSON" class="headerlink" title="Why do you need JSON"></a>Why do you need JSON</h3><p>When exchanging data between a browser and a server, the data can only be text. Since JSON is text only, it can easily be sent to and from a server, and used as a data format by any programming language.</p><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-are-PWAs"><a href="#What-are-PWAs" class="headerlink" title="What are PWAs"></a>What are PWAs</h3><p>Progressive web applications (PWAs) are a type of mobile app delivered through the web, built using common web technologies including HTML, CSS and JavaScript. These PWAs are deployed to servers, accessible through URLs, and indexed by search engines.</p><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-the-purpose-of-clearTimeout-method"><a href="#What-is-the-purpose-of-clearTimeout-method" class="headerlink" title="What is the purpose of clearTimeout method"></a>What is the purpose of clearTimeout method</h3><p>The clearTimeout() function is used in javascript to clear the timeout which has been set by setTimeout()function before that. i.e, The return value of setTimeout() function is stored in a variable and it’s passed into the clearTimeout() function to clear the timer.</p><p>For example, the below setTimeout method is used to display the message after 3 seconds. This timeout can be cleared by the clearTimeout() method.</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">&lt;script&gt;</span><br><span class="line"><span class="keyword">var</span> msg;</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">greeting</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">   alert(<span class="string">&#x27;Good morning&#x27;</span>);</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">start</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">  msg =<span class="built_in">setTimeout</span>(greeting, <span class="number">3000</span>);</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">stop</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="built_in">clearTimeout</span>(msg);</span><br><span class="line">&#125;</span><br><span class="line">&lt;/script&gt;</span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-the-purpose-of-clearInterval-method"><a href="#What-is-the-purpose-of-clearInterval-method" class="headerlink" title="What is the purpose of clearInterval method"></a>What is the purpose of clearInterval method</h3><p>The clearInterval() function is used in javascript to clear the interval which has been set by setInterval() function. i.e, The return value returned by setInterval() function is stored in a variable and it’s passed into the clearInterval() function to clear the interval.</p><p>For example, the below setInterval method is used to display the message for every 3 seconds. This interval can be cleared by the clearInterval() method.</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">&lt;script&gt;</span><br><span class="line"><span class="keyword">var</span> msg;</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">greeting</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">   alert(<span class="string">&#x27;Good morning&#x27;</span>);</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">start</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">  msg = <span class="built_in">setInterval</span>(greeting, <span class="number">3000</span>);</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">stop</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="built_in">clearInterval</span>(msg);</span><br><span class="line">&#125;</span><br><span class="line">&lt;/script&gt;</span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="How-do-you-redirect-new-page-in-javascript"><a href="#How-do-you-redirect-new-page-in-javascript" class="headerlink" title="How do you redirect new page in javascript"></a>How do you redirect new page in javascript</h3><p>In vanilla javascript, you can redirect to a new page using the <code>location</code> property of window object. The syntax would be as follows,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">redirect</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">   <span class="built_in">window</span>.location.href = <span class="string">&#x27;newPage.html&#x27;</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="How-do-you-check-whether-a-string-contains-a-substring"><a href="#How-do-you-check-whether-a-string-contains-a-substring" class="headerlink" title="How do you check whether a string contains a substring"></a>How do you check whether a string contains a substring</h3><p>There are 3 possible ways to check whether a string contains a substring or not,</p><ol><li><strong>Using includes:</strong> ES6 provided <code>String.prototype.includes</code> method to test a string contains a substring</li></ol><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> mainString = <span class="string">&quot;hello&quot;</span>, subString = <span class="string">&quot;hell&quot;</span>;</span><br><span class="line">mainString.includes(subString)</span><br></pre></td></tr></table></figure><ol><li><strong>Using indexOf:</strong> In an ES5 or older environment, you can use <code>String.prototype.indexOf</code> which returns the index of a substring. If the index value is not equal to -1 then it means the substring exists in the main string.</li></ol><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> mainString = <span class="string">&quot;hello&quot;</span>, subString = <span class="string">&quot;hell&quot;</span>;</span><br><span class="line">mainString.indexOf(subString) !== -<span class="number">1</span></span><br></pre></td></tr></table></figure><ol><li><strong>Using RegEx:</strong> The advanced solution is using Regular expression’s test method(<code>RegExp.test</code>), which allows for testing for against regular expressions</li></ol><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> mainString = <span class="string">&quot;hello&quot;</span>, regex = <span class="string">&quot;/hell/&quot;</span>;</span><br><span class="line">regex.test(mainString)</span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="How-do-you-validate-an-email-in-javascript"><a href="#How-do-you-validate-an-email-in-javascript" class="headerlink" title="How do you validate an email in javascript"></a>How do you validate an email in javascript</h3><p>You can validate an email in javascript using regular expressions. It is recommended to do validations on the server side instead of the client side. Because the javascript can be disabled on the client side.</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">validateEmail</span>(<span class="params">email</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">var</span> re = <span class="regexp">/^(([^&lt;&gt;()\[\]\\.,;:\s@&quot;]+(\.[^&lt;&gt;()\[\]\\.,;:\s@&quot;]+)*)|(&quot;.+&quot;))@((\[[0-9]&#123;1,3&#125;\.[0-9]&#123;1,3&#125;\.[0-9]&#123;1,3&#125;\.[0-9]&#123;1,3&#125;\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]&#123;2,&#125;))$/</span>;</span><br><span class="line">    <span class="keyword">return</span> re.test(<span class="built_in">String</span>(email).toLowerCase());</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p><p>The above regular expression accepts unicode characters.</p></li><li><h3 id="How-do-you-get-the-current-url-with-javascript"><a href="#How-do-you-get-the-current-url-with-javascript" class="headerlink" title="How do you get the current url with javascript"></a>How do you get the current url with javascript</h3><p>You can use <code>window.location.href</code> expression to get the current url path and you can use the same expression for updating the URL too. You can also use <code>document.URL</code> for read-only purposes but this solution has issues in FF.</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">console</span>.log(<span class="string">&#x27;location.href&#x27;</span>, <span class="built_in">window</span>.location.href); <span class="comment">// Returns full URL</span></span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-are-the-various-url-properties-of-location-object"><a href="#What-are-the-various-url-properties-of-location-object" class="headerlink" title="What are the various url properties of location object"></a>What are the various url properties of location object</h3><p>The below <code>Location</code> object properties can be used to access URL components of the page,</p><ol><li>href - The entire URL</li><li>protocol - The protocol of the URL</li><li>host - The hostname and port of the URL</li><li>hostname - The hostname of the URL</li><li>port - The port number in the URL</li><li>pathname - The path name of the URL</li><li>search - The query portion of the URL</li><li>hash - The anchor portion of the URL</li></ol><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="How-do-get-query-string-values-in-javascript"><a href="#How-do-get-query-string-values-in-javascript" class="headerlink" title="How do get query string values in javascript"></a>How do get query string values in javascript</h3><p>You can use URLSearchParams to get query string values in javascript. Let’s see an example to get the client code value from URL query string,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> urlParams = <span class="keyword">new</span> URLSearchParams(<span class="built_in">window</span>.location.search);</span><br><span class="line"><span class="keyword">const</span> clientCode = urlParams.get(<span class="string">&#x27;clientCode&#x27;</span>);</span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="How-do-you-check-if-a-key-exists-in-an-object"><a href="#How-do-you-check-if-a-key-exists-in-an-object" class="headerlink" title="How do you check if a key exists in an object"></a>How do you check if a key exists in an object</h3><p>You can check whether a key exists in an object or not using three approaches,</p><ol><li><strong>Using in operator:</strong> You can use the in operator whether a key exists in an object or not</li></ol><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">&quot;key&quot;</span> <span class="keyword">in</span> obj</span><br></pre></td></tr></table></figure><p>and If you want to check if a key doesn’t exist, remember to use parenthesis,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">!(<span class="string">&quot;key&quot;</span> <span class="keyword">in</span> obj)</span><br></pre></td></tr></table></figure><ol><li><strong>Using hasOwnProperty method:</strong> You can use <code>hasOwnProperty</code> to particularly test for properties of the object instance (and not inherited properties)</li></ol><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">obj.hasOwnProperty(<span class="string">&quot;key&quot;</span>) <span class="comment">// true</span></span><br></pre></td></tr></table></figure><ol><li><strong>Using undefined comparison:</strong> If you access a non-existing property from an object, the result is undefined. Let’s compare the properties against undefined to determine the existence of the property.</li></ol><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> user = &#123;</span><br><span class="line">  name: <span class="string">&#x27;John&#x27;</span></span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="built_in">console</span>.log(user.name !== <span class="literal">undefined</span>);     <span class="comment">// true</span></span><br><span class="line"><span class="built_in">console</span>.log(user.nickName !== <span class="literal">undefined</span>); <span class="comment">// false</span></span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="How-do-you-loop-through-or-enumerate-javascript-object"><a href="#How-do-you-loop-through-or-enumerate-javascript-object" class="headerlink" title="How do you loop through or enumerate javascript object"></a>How do you loop through or enumerate javascript object</h3><p>You can use the <code>for-in</code> loop to loop through javascript object. You can also make sure that the key you get is an actual property of an object, and doesn’t come from the prototype using <code>hasOwnProperty</code> method.</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> object = &#123;</span><br><span class="line">    <span class="string">&quot;k1&quot;</span>: <span class="string">&quot;value1&quot;</span>,</span><br><span class="line">    <span class="string">&quot;k2&quot;</span>: <span class="string">&quot;value2&quot;</span>,</span><br><span class="line">    <span class="string">&quot;k3&quot;</span>: <span class="string">&quot;value3&quot;</span></span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="keyword">for</span> (<span class="keyword">var</span> key <span class="keyword">in</span> object) &#123;</span><br><span class="line">    <span class="keyword">if</span> (object.hasOwnProperty(key)) &#123;</span><br><span class="line">        <span class="built_in">console</span>.log(key + <span class="string">&quot; -&gt; &quot;</span> + object[key]); <span class="comment">// k1 -&gt; value1 ...</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="How-do-you-test-for-an-empty-object"><a href="#How-do-you-test-for-an-empty-object" class="headerlink" title="How do you test for an empty object"></a>How do you test for an empty object</h3><p>There are different solutions based on ECMAScript versions</p><ol><li><strong>Using Object entries(ECMA 7+):</strong> You can use object entries length along with constructor type.</li></ol><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">Object</span>.entries(obj).length === <span class="number">0</span> &amp;&amp; obj.constructor === <span class="built_in">Object</span> <span class="comment">// Since date object length is 0, you need to check constructor check as well</span></span><br></pre></td></tr></table></figure><ol><li><strong>Using Object keys(ECMA 5+):</strong> You can use object keys length along with constructor type.</li></ol><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">Object</span>.keys(obj).length === <span class="number">0</span> &amp;&amp; obj.constructor === <span class="built_in">Object</span> <span class="comment">// Since date object length is 0, you need to check constructor check as well</span></span><br></pre></td></tr></table></figure><ol><li><strong>Using for-in with hasOwnProperty(Pre-ECMA 5):</strong> You can use a for-in loop along with hasOwnProperty.</li></ol><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">isEmpty</span>(<span class="params">obj</span>) </span>&#123;</span><br><span class="line">  <span class="keyword">for</span>(<span class="keyword">var</span> prop <span class="keyword">in</span> obj) &#123;</span><br><span class="line">    <span class="keyword">if</span>(obj.hasOwnProperty(prop)) &#123;</span><br><span class="line">      <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">return</span> <span class="built_in">JSON</span>.stringify(obj) === <span class="built_in">JSON</span>.stringify(&#123;&#125;);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-an-arguments-object"><a href="#What-is-an-arguments-object" class="headerlink" title="What is an arguments object"></a>What is an arguments object</h3><p>The arguments object is an Array-like object accessible inside functions that contains the values of the arguments passed to that function. For example, let’s see how to use arguments object inside sum function,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">sum</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="keyword">var</span> total = <span class="number">0</span>;</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>, len = <span class="built_in">arguments</span>.length; i &lt; len; ++i) &#123;</span><br><span class="line">        total += <span class="built_in">arguments</span>[i];</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> total;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">sum(<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>) <span class="comment">// returns 6</span></span><br></pre></td></tr></table></figure><p><strong>Note:</strong> You can’t apply array methods on arguments object. But you can convert into a regular array as below.</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> argsArray = <span class="built_in">Array</span>.prototype.slice.call(<span class="built_in">arguments</span>);</span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="How-do-you-make-first-letter-of-the-string-in-an-uppercase"><a href="#How-do-you-make-first-letter-of-the-string-in-an-uppercase" class="headerlink" title="How do you make first letter of the string in an uppercase"></a>How do you make first letter of the string in an uppercase</h3><p>You can create a function which uses a chain of string methods such as charAt, toUpperCase and slice methods to generate a string with the first letter in uppercase.</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">capitalizeFirstLetter</span>(<span class="params">string</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> string.charAt(<span class="number">0</span>).toUpperCase() + string.slice(<span class="number">1</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-are-the-pros-and-cons-of-for-loop"><a href="#What-are-the-pros-and-cons-of-for-loop" class="headerlink" title="What are the pros and cons of for loop"></a>What are the pros and cons of for loop</h3><p>The for-loop is a commonly used iteration syntax in javascript. It has both pros and cons<br>####Pros</p><ol><li>Works on every environment</li><li>You can use break and continue flow control statements</li></ol><p>####Cons</p><ol><li>Too verbose</li><li>Imperative</li><li>You might face one-by-off errors</li></ol><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="How-do-you-display-the-current-date-in-javascript"><a href="#How-do-you-display-the-current-date-in-javascript" class="headerlink" title="How do you display the current date in javascript"></a>How do you display the current date in javascript</h3><p>You can use <code>new Date()</code> to generate a new Date object containing the current date and time. For example, let’s display the current date in mm/dd/yyyy</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> today = <span class="keyword">new</span> <span class="built_in">Date</span>();</span><br><span class="line"><span class="keyword">var</span> dd = <span class="built_in">String</span>(today.getDate()).padStart(<span class="number">2</span>, <span class="string">&#x27;0&#x27;</span>);</span><br><span class="line"><span class="keyword">var</span> mm = <span class="built_in">String</span>(today.getMonth() + <span class="number">1</span>).padStart(<span class="number">2</span>, <span class="string">&#x27;0&#x27;</span>); <span class="comment">//January is 0!</span></span><br><span class="line"><span class="keyword">var</span> yyyy = today.getFullYear();</span><br><span class="line"></span><br><span class="line">today = mm + <span class="string">&#x27;/&#x27;</span> + dd + <span class="string">&#x27;/&#x27;</span> + yyyy;</span><br><span class="line"><span class="built_in">document</span>.write(today);</span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="How-do-you-compare-two-date-objects"><a href="#How-do-you-compare-two-date-objects" class="headerlink" title="How do you compare two date objects"></a>How do you compare two date objects</h3><p>You need to use date.getTime() method to compare date values instead of comparison operators (==, !=, ===, and !== operators)</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> d1 = <span class="keyword">new</span> <span class="built_in">Date</span>();</span><br><span class="line"><span class="keyword">var</span> d2 = <span class="keyword">new</span> <span class="built_in">Date</span>(d1);</span><br><span class="line"><span class="built_in">console</span>.log(d1.getTime() === d2.getTime()); <span class="comment">//True</span></span><br><span class="line"><span class="built_in">console</span>.log(d1 === d2); <span class="comment">// False</span></span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="How-do-you-check-if-a-string-starts-with-another-string"><a href="#How-do-you-check-if-a-string-starts-with-another-string" class="headerlink" title="How do you check if a string starts with another string"></a>How do you check if a string starts with another string</h3><p>You can use ECMAScript 6’s <code>String.prototype.startsWith()</code> method to check if a string starts with another string or not. But it is not yet supported in all browsers. Let’s see an example to see this usage,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">&quot;Good morning&quot;</span>.startsWith(<span class="string">&quot;Good&quot;</span>); <span class="comment">// true</span></span><br><span class="line"><span class="string">&quot;Good morning&quot;</span>.startsWith(<span class="string">&quot;morning&quot;</span>); <span class="comment">// false</span></span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="How-do-you-trim-a-string-in-javascript"><a href="#How-do-you-trim-a-string-in-javascript" class="headerlink" title="How do you trim a string in javascript"></a>How do you trim a string in javascript</h3><p>JavaScript provided a trim method on string types to trim any whitespaces present at the beginning or ending of the string.</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">&quot;  Hello World   &quot;</span>.trim(); <span class="comment">//Hello World</span></span><br></pre></td></tr></table></figure><p>If your browser(&lt;IE9) doesn’t support this method then you can use below polyfill.</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">if</span> (!<span class="built_in">String</span>.prototype.trim) &#123;</span><br><span class="line">    (<span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">        <span class="comment">// Make sure we trim BOM and NBSP</span></span><br><span class="line">        <span class="keyword">var</span> rtrim = <span class="regexp">/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g</span>;</span><br><span class="line">        <span class="built_in">String</span>.prototype.trim = <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="built_in">this</span>.replace(rtrim, <span class="string">&#x27;&#x27;</span>);</span><br><span class="line">        &#125;;</span><br><span class="line">    &#125;)();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="How-do-you-add-a-key-value-pair-in-javascript"><a href="#How-do-you-add-a-key-value-pair-in-javascript" class="headerlink" title="How do you add a key value pair in javascript"></a>How do you add a key value pair in javascript</h3><p>There are two possible solutions to add new properties to an object. Let’s take a simple object to explain these solutions.</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> object = &#123;</span><br><span class="line">    key1: value1,</span><br><span class="line">    key2: value2</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure><ol><li><strong>Using dot notation:</strong> This solution is useful when you know the name of the property</li></ol><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">object.key3 = <span class="string">&quot;value3&quot;</span>;</span><br></pre></td></tr></table></figure><ol><li><strong>Using square bracket notation:</strong> This solution is useful when the name of the property is dynamically determined.</li></ol><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">obj[<span class="string">&quot;key3&quot;</span>] = <span class="string">&quot;value3&quot;</span>;</span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="Is-the-–-notation-represents-a-special-operator"><a href="#Is-the-–-notation-represents-a-special-operator" class="headerlink" title="Is the !– notation represents a special operator"></a>Is the !– notation represents a special operator</h3><p>No,that’s not a special operator. But it is a combination of 2 standard operators one after the other,</p><ol><li>A logical not (!)</li><li>A prefix decrement (–)</li></ol><p>At first, the value decremented by one and then tested to see if it is equal to zero or not for determining the truthy/falsy value.</p><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="How-do-you-assign-default-values-to-variables"><a href="#How-do-you-assign-default-values-to-variables" class="headerlink" title="How do you assign default values to variables"></a>How do you assign default values to variables</h3><p>You can use the logical or operator <code>||</code> in an assignment expression to provide a default value. The syntax looks like as below,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> a = b || c;</span><br></pre></td></tr></table></figure><p>As per the above expression, variable ‘a ‘will get the value of ‘c’ only if ‘b’ is falsy (if is null, false, undefined, 0, empty string, or NaN), otherwise ‘a’ will get the value of ‘b’.</p><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="How-do-you-define-multiline-strings"><a href="#How-do-you-define-multiline-strings" class="headerlink" title="How do you define multiline strings"></a>How do you define multiline strings</h3><p>You can define multiline string literals using the ‘&#39; character followed by line terminator.</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> str = <span class="string">&quot;This is a \</span></span><br><span class="line"><span class="string">very lengthy \</span></span><br><span class="line"><span class="string">sentence!&quot;</span>;</span><br></pre></td></tr></table></figure><p>But if you have a space after the ‘&#39; character, the code will look exactly the same, but it will raise a SyntaxError.</p><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-an-app-shell-model"><a href="#What-is-an-app-shell-model" class="headerlink" title="What is an app shell model"></a>What is an app shell model</h3><p>An application shell (or app shell) architecture is one way to build a Progressive Web App that reliably and instantly loads on your users’ screens, similar to what you see in native applications. It is useful for getting some initial HTML to the screen fast without a network.</p><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="Can-we-define-properties-for-functions"><a href="#Can-we-define-properties-for-functions" class="headerlink" title="Can we define properties for functions"></a>Can we define properties for functions</h3><p>Yes, We can define properties for functions because functions are also objects.</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">fn = <span class="function"><span class="keyword">function</span>(<span class="params">x</span>) </span>&#123;</span><br><span class="line">   <span class="comment">//Function code goes here</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">fn.name = <span class="string">&quot;John&quot;</span>;</span><br><span class="line"></span><br><span class="line">fn.profile = <span class="function"><span class="keyword">function</span>(<span class="params">y</span>) </span>&#123;</span><br><span class="line">  <span class="comment">//Profile code goes here</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-the-way-to-find-the-number-of-parameters-expected-by-a-function"><a href="#What-is-the-way-to-find-the-number-of-parameters-expected-by-a-function" class="headerlink" title="What is the way to find the number of parameters expected by a function"></a>What is the way to find the number of parameters expected by a function</h3><p>You can use <code>function.length</code> syntax to find the number of parameters expected by a function. Let’s take an example of <code>sum</code> function to calculate the sum of numbers,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">sum</span>(<span class="params">num1, num2, num3, num4</span>)</span>&#123;</span><br><span class="line">    <span class="keyword">return</span> num1 + num2 + num3 + num4;</span><br><span class="line">&#125;</span><br><span class="line">sum.length <span class="comment">// 4 is the number of parameters expected.</span></span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-a-polyfill"><a href="#What-is-a-polyfill" class="headerlink" title="What is a polyfill"></a>What is a polyfill</h3><p>A polyfill is a piece of JS code used to provide modern functionality on older browsers that do not natively support it. For example, Silverlight plugin polyfill can be used to mimic the functionality of an HTML Canvas element on Microsoft Internet Explorer 7.</p><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-are-break-and-continue-statements"><a href="#What-are-break-and-continue-statements" class="headerlink" title="What are break and continue statements"></a>What are break and continue statements</h3><p>The break statement is used to “jump out” of a loop. i.e, It breaks the loop and continues executing the code after the loop.</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">for</span> (i = <span class="number">0</span>; i &lt; <span class="number">10</span>; i++) &#123;</span><br><span class="line">  <span class="keyword">if</span> (i === <span class="number">5</span>) &#123; <span class="keyword">break</span>; &#125;</span><br><span class="line">  text += <span class="string">&quot;Number: &quot;</span> + i + <span class="string">&quot;&lt;br&gt;&quot;</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>The continue statement is used to “jump over” one iteration in the loop. i.e, It breaks one iteration (in the loop), if a specified condition occurs, and continues with the next iteration in the loop.</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">for</span> (i = <span class="number">0</span>; i &lt; <span class="number">10</span>; i++) &#123;</span><br><span class="line">    <span class="keyword">if</span> (i === <span class="number">5</span>) &#123; <span class="keyword">continue</span>; &#125;</span><br><span class="line">    text += <span class="string">&quot;Number: &quot;</span> + i + <span class="string">&quot;&lt;br&gt;&quot;</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-are-js-labels"><a href="#What-are-js-labels" class="headerlink" title="What are js labels"></a>What are js labels</h3><p>The label statement allows us to name loops and blocks in JavaScript. We can then use these labels to refer back to the code later. For example, the below code with labels avoids printing the numbers when they are same,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> i, j;</span><br><span class="line"></span><br><span class="line">loop1:</span><br><span class="line"><span class="keyword">for</span> (i = <span class="number">0</span>; i &lt; <span class="number">3</span>; i++) &#123;</span><br><span class="line">   loop2:</span><br><span class="line">   <span class="keyword">for</span> (j = <span class="number">0</span>; j &lt; <span class="number">3</span>; j++) &#123;</span><br><span class="line">      <span class="keyword">if</span> (i === j) &#123;</span><br><span class="line">         <span class="keyword">continue</span> loop1;</span><br><span class="line">      &#125;</span><br><span class="line">      <span class="built_in">console</span>.log(<span class="string">&#x27;i = &#x27;</span> + i + <span class="string">&#x27;, j = &#x27;</span> + j);</span><br><span class="line">   &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// Output is:</span></span><br><span class="line"><span class="comment">//   &quot;i = 1, j = 0&quot;</span></span><br><span class="line"><span class="comment">//   &quot;i = 2, j = 0&quot;</span></span><br><span class="line"><span class="comment">//   &quot;i = 2, j = 1&quot;</span></span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-are-the-benefits-of-keeping-declarations-at-the-top"><a href="#What-are-the-benefits-of-keeping-declarations-at-the-top" class="headerlink" title="What are the benefits of keeping declarations at the top"></a>What are the benefits of keeping declarations at the top</h3><p>It is recommended to keep all declarations at the top of each script or function. The benefits of doing this are,</p><ol><li>Gives cleaner code</li><li>It provides a single place to look for local variables</li><li>Easy to avoid unwanted global variables</li><li>It reduces the possibility of unwanted re-declarations</li></ol><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-are-the-benefits-of-initializing-variables"><a href="#What-are-the-benefits-of-initializing-variables" class="headerlink" title="What are the benefits of initializing variables"></a>What are the benefits of initializing variables</h3><p>It is recommended to initialize variables because of the below benefits,</p><ol><li>It gives cleaner code</li><li>It provides a single place to initialize variables</li><li>Avoid undefined values in the code</li></ol><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-are-the-recommendations-to-create-new-object"><a href="#What-are-the-recommendations-to-create-new-object" class="headerlink" title="What are the recommendations to create new object"></a>What are the recommendations to create new object</h3><p>It is recommended to avoid creating new objects using <code>new Object()</code>. Instead you can initialize values based on it’s type to create the objects.</p><ol><li>Assign {} instead of new Object()</li><li>Assign “” instead of new String()</li><li>Assign 0 instead of new Number()</li><li>Assign false instead of new Boolean()</li><li>Assign [] instead of new Array()</li><li>Assign /()/ instead of new RegExp()</li><li>Assign function (){} instead of new Function()</li></ol><p>You can define them as an example,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> v1 = &#123;&#125;;</span><br><span class="line"><span class="keyword">var</span> v2 = <span class="string">&quot;&quot;</span>;</span><br><span class="line"><span class="keyword">var</span> v3 = <span class="number">0</span>;</span><br><span class="line"><span class="keyword">var</span> v4 = <span class="literal">false</span>;</span><br><span class="line"><span class="keyword">var</span> v5 = [];</span><br><span class="line"><span class="keyword">var</span> v6 = <span class="regexp">/()/</span>;</span><br><span class="line"><span class="keyword">var</span> v7 = <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;&#125;;</span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="How-do-you-define-JSON-arrays"><a href="#How-do-you-define-JSON-arrays" class="headerlink" title="How do you define JSON arrays"></a>How do you define JSON arrays</h3><p>JSON arrays are written inside square brackets and arrays contain javascript objects. For example, the JSON array of users would be as below,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">&quot;users&quot;</span>:[</span><br><span class="line">  &#123;<span class="string">&quot;firstName&quot;</span>:<span class="string">&quot;John&quot;</span>, <span class="string">&quot;lastName&quot;</span>:<span class="string">&quot;Abrahm&quot;</span>&#125;,</span><br><span class="line">  &#123;<span class="string">&quot;firstName&quot;</span>:<span class="string">&quot;Anna&quot;</span>, <span class="string">&quot;lastName&quot;</span>:<span class="string">&quot;Smith&quot;</span>&#125;,</span><br><span class="line">  &#123;<span class="string">&quot;firstName&quot;</span>:<span class="string">&quot;Shane&quot;</span>, <span class="string">&quot;lastName&quot;</span>:<span class="string">&quot;Warn&quot;</span>&#125;</span><br><span class="line">]</span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="How-do-you-generate-random-integers"><a href="#How-do-you-generate-random-integers" class="headerlink" title="How do you generate random integers"></a>How do you generate random integers</h3><p>You can use Math.random() with Math.floor() to return random integers. For example, if you want generate random integers between 1 to 10, the multiplication factor should be 10,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">Math</span>.floor(<span class="built_in">Math</span>.random() * <span class="number">10</span>) + <span class="number">1</span>;     <span class="comment">// returns a random integer from 1 to 10</span></span><br><span class="line"><span class="built_in">Math</span>.floor(<span class="built_in">Math</span>.random() * <span class="number">100</span>) + <span class="number">1</span>;     <span class="comment">// returns a random integer from 1 to 100</span></span><br></pre></td></tr></table></figure><p><strong>Note:</strong> Math.random() returns a random number between 0 (inclusive), and 1 (exclusive)</p><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="Can-you-write-a-random-integers-function-to-print-integers-with-in-a-range"><a href="#Can-you-write-a-random-integers-function-to-print-integers-with-in-a-range" class="headerlink" title="Can you write a random integers function to print integers with in a range"></a>Can you write a random integers function to print integers with in a range</h3><p>Yes, you can create a proper random function to return a random number between min and max (both included)</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">randomInteger</span>(<span class="params">min, max</span>) </span>&#123;</span><br><span class="line">  <span class="keyword">return</span> <span class="built_in">Math</span>.floor(<span class="built_in">Math</span>.random() * (max - min + <span class="number">1</span>) ) + min;</span><br><span class="line">&#125;</span><br><span class="line">randomInteger(<span class="number">1</span>, <span class="number">100</span>); <span class="comment">// returns a random integer from 1 to 100</span></span><br><span class="line">randomInteger(<span class="number">1</span>, <span class="number">1000</span>); <span class="comment">// returns a random integer from 1 to 1000</span></span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-tree-shaking"><a href="#What-is-tree-shaking" class="headerlink" title="What is tree shaking"></a>What is tree shaking</h3><p>Tree shaking is a form of dead code elimination. It means that unused modules will not be included in the bundle during the build process and for that it relies on the static structure of ES2015 module syntax,( i.e. import and export). Initially this has been popularized by the ES2015 module bundler <code>rollup</code>.</p><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-the-need-of-tree-shaking"><a href="#What-is-the-need-of-tree-shaking" class="headerlink" title="What is the need of tree shaking"></a>What is the need of tree shaking</h3><p>Tree Shaking can significantly reduce the code size in any application. i.e, The less code we send over the wire the more performant the application will be. For example, if we just want to create a “Hello World” Application using SPA frameworks then it will take around a few MBs, but by tree shaking it can bring down the size to just a few hundred KBs. Tree shaking is implemented in Rollup and Webpack bundlers.</p><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="Is-it-recommended-to-use-eval"><a href="#Is-it-recommended-to-use-eval" class="headerlink" title="Is it recommended to use eval"></a>Is it recommended to use eval</h3><p>No, it allows arbitrary code to be run which causes a security problem. As we know that the eval() function is used to run text as code. In most of the cases, it should not be necessary to use it.</p><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-a-Regular-Expression"><a href="#What-is-a-Regular-Expression" class="headerlink" title="What is a Regular Expression"></a>What is a Regular Expression</h3><p>A regular expression is a sequence of characters that forms a search pattern. You can use this search pattern for searching data in a text. These can be used to perform all types of text search and text replace operations. Let’s see the syntax format now,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">/pattern/modifiers;</span><br></pre></td></tr></table></figure><p>For example, the regular expression or search pattern with case-insensitive username would be,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">/John/i</span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-are-the-string-methods-available-in-Regular-expression"><a href="#What-are-the-string-methods-available-in-Regular-expression" class="headerlink" title="What are the string methods available in Regular expression"></a>What are the string methods available in Regular expression</h3><p>Regular Expressions has two string methods: search() and replace().<br>The search() method uses an expression to search for a match, and returns the position of the match.</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> msg = <span class="string">&quot;Hello John&quot;</span>;</span><br><span class="line"><span class="keyword">var</span> n = msg.search(<span class="regexp">/John/i</span>); <span class="comment">// 6</span></span><br></pre></td></tr></table></figure><p>The replace() method is used to return a modified string where the pattern is replaced.</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> msg = <span class="string">&quot;Hello John&quot;</span>;</span><br><span class="line"><span class="keyword">var</span> n = msg.replace(<span class="regexp">/John/i</span>, <span class="string">&quot;Buttler&quot;</span>); <span class="comment">// Hello Buttler</span></span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-are-modifiers-in-regular-expression"><a href="#What-are-modifiers-in-regular-expression" class="headerlink" title="What are modifiers in regular expression"></a>What are modifiers in regular expression</h3><p>Modifiers can be used to perform case-insensitive and global searches. Let’s list down some of the modifiers,</p><table><thead><tr><th>Modifier</th><th>Description</th></tr></thead><tbody><tr><td>i</td><td>Perform case-insensitive matching</td></tr><tr><td>g</td><td>Perform a global match rather than stops at first match</td></tr><tr><td>m</td><td>Perform multiline matching</td></tr></tbody></table><p>Let’s take an example of global modifier,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> text = <span class="string">&quot;Learn JS one by one&quot;</span>;</span><br><span class="line"><span class="keyword">var</span> pattern = <span class="regexp">/one/g</span>;</span><br><span class="line"><span class="keyword">var</span> result = text.match(pattern); <span class="comment">// one,one</span></span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-are-regular-expression-patterns"><a href="#What-are-regular-expression-patterns" class="headerlink" title="What are regular expression patterns"></a>What are regular expression patterns</h3><p>Regular Expressions provide a group of patterns in order to match characters. Basically they are categorized into 3 types,</p><ol><li><strong>Brackets:</strong> These are used to find a range of characters.<br>For example, below are some use cases,<ol><li>[abc]: Used to find any of the characters between the brackets(a,b,c)</li><li>[0-9]: Used to find any of the digits between the brackets</li><li>(a|b): Used to find any of the alternatives separated with |</li></ol></li><li><strong>Metacharacters:</strong> These are characters with a special meaning<br>For example, below are some use cases,<ol><li>\d: Used to find a digit</li><li>\s: Used to find a whitespace character</li><li>\b: Used to find a match at the beginning or ending of a word</li></ol></li><li><strong>Quantifiers:</strong> These are useful to define quantities<br>For example, below are some use cases,<ol><li>n+: Used to find matches for any string that contains at least one n</li><li>n*: Used to find matches for any string that contains zero or more occurrences of n</li><li>n?: Used to find matches for any string that contains zero or one occurrences of n</li></ol></li></ol><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-a-RegExp-object"><a href="#What-is-a-RegExp-object" class="headerlink" title="What is a RegExp object"></a>What is a RegExp object</h3><p>RegExp object is a regular expression object with predefined properties and methods. Let’s see the simple usage of RegExp object,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> regexp = <span class="keyword">new</span> <span class="built_in">RegExp</span>(<span class="string">&#x27;\\w+&#x27;</span>);</span><br><span class="line"><span class="built_in">console</span>.log(regexp);</span><br><span class="line"><span class="comment">// expected output: /\w+/</span></span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="How-do-you-search-a-string-for-a-pattern"><a href="#How-do-you-search-a-string-for-a-pattern" class="headerlink" title="How do you search a string for a pattern"></a>How do you search a string for a pattern</h3><p>You can use the test() method of regular expression in order to search a string for a pattern, and return true or false depending on the result.</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> pattern = <span class="regexp">/you/</span>;</span><br><span class="line"><span class="built_in">console</span>.log(pattern.test(<span class="string">&quot;How are you?&quot;</span>)); <span class="comment">//true</span></span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-the-purpose-of-exec-method"><a href="#What-is-the-purpose-of-exec-method" class="headerlink" title="What is the purpose of exec method"></a>What is the purpose of exec method</h3><p>The purpose of exec method is similar to test method but it executes a search for a match in a specified string and returns a result array, or null instead of returning true/false.</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> pattern = <span class="regexp">/you/</span>;</span><br><span class="line"><span class="built_in">console</span>.log(pattern.exec(<span class="string">&quot;How are you?&quot;</span>)); <span class="comment">//[&quot;you&quot;, index: 8, input: &quot;How are you?&quot;, groups: undefined]</span></span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="How-do-you-change-the-style-of-a-HTML-element"><a href="#How-do-you-change-the-style-of-a-HTML-element" class="headerlink" title="How do you change the style of a HTML element"></a>How do you change the style of a HTML element</h3><p>You can change inline style or classname of a HTML element using javascript</p><ol><li><strong>Using style property:</strong> You can modify inline style using style property</li></ol><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">document</span>.getElementById(<span class="string">&quot;title&quot;</span>).style.fontSize = <span class="string">&quot;30px&quot;</span>;</span><br></pre></td></tr></table></figure><ol><li><strong>Using ClassName property:</strong> It is easy to modify element class using className property</li></ol><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">document</span>.getElementById(<span class="string">&quot;title&quot;</span>).style.className = <span class="string">&quot;custom-title&quot;</span>;</span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-would-be-the-result-of-1-2-’3’"><a href="#What-would-be-the-result-of-1-2-’3’" class="headerlink" title="What would be the result of 1+2+’3’"></a>What would be the result of 1+2+’3’</h3><p>The output is going to be <code>33</code>. Since <code>1</code> and <code>2</code> are numeric values, the result of the first two digits is going to be a numeric value <code>3</code>. The next digit is a string type value because of that the addition of numeric value <code>3</code> and string type value <code>3</code> is just going to be a concatenation value <code>33</code>.</p><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-a-debugger-statement"><a href="#What-is-a-debugger-statement" class="headerlink" title="What is a debugger statement"></a>What is a debugger statement</h3><p>The debugger statement invokes any available debugging functionality, such as setting a breakpoint. If no debugging functionality is available, this statement has no effect.<br>For example, in the below function a debugger statement has been inserted. So<br>execution is paused at the debugger statement just like a breakpoint in the script source.</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">getProfile</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line"><span class="comment">// code goes here</span></span><br><span class="line"><span class="keyword">debugger</span>;</span><br><span class="line"><span class="comment">// code goes here</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-the-purpose-of-breakpoints-in-debugging"><a href="#What-is-the-purpose-of-breakpoints-in-debugging" class="headerlink" title="What is the purpose of breakpoints in debugging"></a>What is the purpose of breakpoints in debugging</h3><p>You can set breakpoints in the javascript code once the debugger statement is executed and the debugger window pops up. At each breakpoint, javascript will stop executing, and let you examine the JavaScript values. After examining values, you can resume the execution of code using the play button.</p><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="Can-I-use-reserved-words-as-identifiers"><a href="#Can-I-use-reserved-words-as-identifiers" class="headerlink" title="Can I use reserved words as identifiers"></a>Can I use reserved words as identifiers</h3><p>No, you cannot use the reserved words as variables, labels, object or function names. Let’s see one simple example,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> <span class="keyword">else</span> = <span class="string">&quot;hello&quot;</span>; <span class="comment">// Uncaught SyntaxError: Unexpected token else</span></span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="How-do-you-detect-a-mobile-browser"><a href="#How-do-you-detect-a-mobile-browser" class="headerlink" title="How do you detect a mobile browser"></a>How do you detect a mobile browser</h3><p>You can use regex which returns a true or false value depending on whether or not the user is browsing with a mobile.</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">window</span>.mobilecheck = <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">  <span class="keyword">var</span> mobileCheck = <span class="literal">false</span>;</span><br><span class="line">  (<span class="function"><span class="keyword">function</span>(<span class="params">a</span>)</span>&#123;<span class="keyword">if</span>(<span class="regexp">/(android|bb\d+|meego).+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|mobile.+firefox|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|series(4|6)0|symbian|treo|up\.(browser|link)|vodafone|wap|windows ce|xda|xiino/i</span>.test(a)||<span class="regexp">/1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s\-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|\-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw\-(n|u)|c55\/|capi|ccwa|cdm\-|cell|chtm|cldc|cmd\-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc\-s|devi|dica|dmob|do(c|p)o|ds(12|\-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(\-|_)|g1 u|g560|gene|gf\-5|g\-mo|go(\.w|od)|gr(ad|un)|haie|hcit|hd\-(m|p|t)|hei\-|hi(pt|ta)|hp( i|ip)|hs\-c|ht(c(\-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i\-(20|go|ma)|i230|iac( |\-|\/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |\/)|klon|kpt |kwc\-|kyo(c|k)|le(no|xi)|lg( g|\/(k|l|u)|50|54|\-[a-w])|libw|lynx|m1\-w|m3ga|m50\/|ma(te|ui|xo)|mc(01|21|ca)|m\-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(\-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)\-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|\-([1-8]|c))|phil|pire|pl(ay|uc)|pn\-2|po(ck|rt|se)|prox|psio|pt\-g|qa\-a|qc(07|12|21|32|60|\-[2-7]|i\-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55\/|sa(ge|ma|mm|ms|ny|va)|sc(01|h\-|oo|p\-)|sdk\/|se(c(\-|0|1)|47|mc|nd|ri)|sgh\-|shar|sie(\-|m)|sk\-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h\-|v\-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl\-|tdg\-|tel(i|m)|tim\-|t\-mo|to(pl|sh)|ts(70|m\-|m3|m5)|tx\-9|up(\.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|\-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(\-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas\-|your|zeto|zte\-/i</span>.test(a.substr(<span class="number">0</span>,<span class="number">4</span>))) mobileCheck = <span class="literal">true</span>;&#125;)(navigator.userAgent||navigator.vendor||<span class="built_in">window</span>.opera);</span><br><span class="line">  <span class="keyword">return</span> mobileCheck;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="How-do-you-detect-a-mobile-browser-without-regexp"><a href="#How-do-you-detect-a-mobile-browser-without-regexp" class="headerlink" title="How do you detect a mobile browser without regexp"></a>How do you detect a mobile browser without regexp</h3><p>You can detect mobile browsers by simply running through a list of devices and checking if the useragent matches anything. This is an alternative solution for RegExp usage,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">detectmob</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line"> <span class="keyword">if</span>( navigator.userAgent.match(<span class="regexp">/Android/i</span>)</span><br><span class="line"> || navigator.userAgent.match(<span class="regexp">/webOS/i</span>)</span><br><span class="line"> || navigator.userAgent.match(<span class="regexp">/iPhone/i</span>)</span><br><span class="line"> || navigator.userAgent.match(<span class="regexp">/iPad/i</span>)</span><br><span class="line"> || navigator.userAgent.match(<span class="regexp">/iPod/i</span>)</span><br><span class="line"> || navigator.userAgent.match(<span class="regexp">/BlackBerry/i</span>)</span><br><span class="line"> || navigator.userAgent.match(<span class="regexp">/Windows Phone/i</span>)</span><br><span class="line"> )&#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">  &#125;</span><br><span class="line"> <span class="keyword">else</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="How-do-you-get-the-image-width-and-height-using-JS"><a href="#How-do-you-get-the-image-width-and-height-using-JS" class="headerlink" title="How do you get the image width and height using JS"></a>How do you get the image width and height using JS</h3><p>You can programmatically get the image and check the dimensions(width and height) using Javascript.</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> img = <span class="keyword">new</span> Image();</span><br><span class="line">img.onload = <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">  <span class="built_in">console</span>.log(<span class="built_in">this</span>.width + <span class="string">&#x27;x&#x27;</span> + <span class="built_in">this</span>.height);</span><br><span class="line">&#125;</span><br><span class="line">img.src = <span class="string">&#x27;http://www.google.com/intl/en_ALL/images/logo.gif&#x27;</span>;</span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="How-do-you-make-synchronous-HTTP-request"><a href="#How-do-you-make-synchronous-HTTP-request" class="headerlink" title="How do you make synchronous HTTP request"></a>How do you make synchronous HTTP request</h3><p>Browsers provide an XMLHttpRequest object which can be used to make synchronous HTTP requests from JavaScript</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">httpGet</span>(<span class="params">theUrl</span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">var</span> xmlHttpReq = <span class="keyword">new</span> XMLHttpRequest();</span><br><span class="line">    xmlHttpReq.open( <span class="string">&quot;GET&quot;</span>, theUrl, <span class="literal">false</span> ); <span class="comment">// false for synchronous request</span></span><br><span class="line">    xmlHttpReq.send( <span class="literal">null</span> );</span><br><span class="line">    <span class="keyword">return</span> xmlHttpReq.responseText;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="How-do-you-make-asynchronous-HTTP-request"><a href="#How-do-you-make-asynchronous-HTTP-request" class="headerlink" title="How do you make asynchronous HTTP request"></a>How do you make asynchronous HTTP request</h3><p>Browsers provide an XMLHttpRequest object which can be used to make asynchronous HTTP requests from JavaScript by passing the 3rd parameter as true.</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">httpGetAsync</span>(<span class="params">theUrl, callback</span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">var</span> xmlHttpReq = <span class="keyword">new</span> XMLHttpRequest();</span><br><span class="line">    xmlHttpReq.onreadystatechange = <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (xmlHttpReq.readyState == <span class="number">4</span> &amp;&amp; xmlHttpReq.status == <span class="number">200</span>)</span><br><span class="line">            callback(xmlHttpReq.responseText);</span><br><span class="line">    &#125;</span><br><span class="line">    xmlHttp.open(<span class="string">&quot;GET&quot;</span>, theUrl, <span class="literal">true</span>); <span class="comment">// true for asynchronous</span></span><br><span class="line">    xmlHttp.send(<span class="literal">null</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="How-do-you-convert-date-to-another-timezone-in-javascript"><a href="#How-do-you-convert-date-to-another-timezone-in-javascript" class="headerlink" title="How do you convert date to another timezone in javascript"></a>How do you convert date to another timezone in javascript</h3><p>You can use the toLocaleString() method to convert dates in one timezone to another. For example, let’s convert current date to British English timezone as below,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">console</span>.log(event.toLocaleString(<span class="string">&#x27;en-GB&#x27;</span>, &#123; <span class="attr">timeZone</span>: <span class="string">&#x27;UTC&#x27;</span> &#125;)); <span class="comment">//29/06/2019, 09:56:00</span></span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-are-the-properties-used-to-get-size-of-window"><a href="#What-are-the-properties-used-to-get-size-of-window" class="headerlink" title="What are the properties used to get size of window"></a>What are the properties used to get size of window</h3><p>You can use innerWidth, innerHeight, clientWidth, clientHeight properties of windows, document element and document body objects to find the size of a window. Let’s use them combination of these properties to calculate the size of a window or document,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> width = <span class="built_in">window</span>.innerWidth</span><br><span class="line">|| <span class="built_in">document</span>.documentElement.clientWidth</span><br><span class="line">|| <span class="built_in">document</span>.body.clientWidth;</span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> height = <span class="built_in">window</span>.innerHeight</span><br><span class="line">|| <span class="built_in">document</span>.documentElement.clientHeight</span><br><span class="line">|| <span class="built_in">document</span>.body.clientHeight;</span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-a-conditional-operator-in-javascript"><a href="#What-is-a-conditional-operator-in-javascript" class="headerlink" title="What is a conditional operator in javascript"></a>What is a conditional operator in javascript</h3><p>The conditional (ternary) operator is the only JavaScript operator that takes three operands which acts as a shortcut for if statements.</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> isAuthenticated = <span class="literal">false</span>;</span><br><span class="line"><span class="built_in">console</span>.log(isAuthenticated ? <span class="string">&#x27;Hello, welcome&#x27;</span> : <span class="string">&#x27;Sorry, you are not authenticated&#x27;</span>); <span class="comment">//Sorry, you are not authenticated</span></span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="Can-you-apply-chaining-on-conditional-operator"><a href="#Can-you-apply-chaining-on-conditional-operator" class="headerlink" title="Can you apply chaining on conditional operator"></a>Can you apply chaining on conditional operator</h3><p>Yes, you can apply chaining on conditional operators similar to if … else if … else if … else chain. The syntax is going to be as below,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">traceValue</span>(<span class="params">someParam</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> condition1 ? value1</span><br><span class="line">         : condition2 ? value2</span><br><span class="line">         : condition3 ? value3</span><br><span class="line">         : value4;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// The above conditional operator is equivalent to:</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">traceValue</span>(<span class="params">someParam</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">if</span> (condition1) &#123; <span class="keyword">return</span> value1; &#125;</span><br><span class="line">    <span class="keyword">else</span> <span class="keyword">if</span> (condition2) &#123; <span class="keyword">return</span> value2; &#125;</span><br><span class="line">    <span class="keyword">else</span> <span class="keyword">if</span> (condition3) &#123; <span class="keyword">return</span> value3; &#125;</span><br><span class="line">    <span class="keyword">else</span> &#123; <span class="keyword">return</span> value4; &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-are-the-ways-to-execute-javascript-after-page-load"><a href="#What-are-the-ways-to-execute-javascript-after-page-load" class="headerlink" title="What are the ways to execute javascript after page load"></a>What are the ways to execute javascript after page load</h3><p>You can execute javascript after page load in many different ways,</p><ol><li><strong>window.onload:</strong></li></ol><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">window</span>.onload = <span class="function"><span class="keyword">function</span> ...</span></span><br></pre></td></tr></table></figure><ol><li><strong>document.onload:</strong></li></ol><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">document</span>.onload = <span class="function"><span class="keyword">function</span> ...</span></span><br></pre></td></tr></table></figure><ol><li><strong>body onload:</strong></li></ol><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">&lt;body onload=<span class="string">&quot;script();&quot;</span>&gt;</span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-the-difference-between-proto-and-prototype"><a href="#What-is-the-difference-between-proto-and-prototype" class="headerlink" title="What is the difference between proto and prototype"></a>What is the difference between proto and prototype</h3><p>The <code>__proto__</code> object is the actual object that is used in the lookup chain to resolve methods, etc. Whereas <code>prototype</code> is the object that is used to build <code>__proto__</code> when you create an object with new</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">( <span class="keyword">new</span> Employee ).__proto__ === Employee.prototype;</span><br><span class="line">( <span class="keyword">new</span> Employee ).prototype === <span class="literal">undefined</span>;</span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="Give-an-example-where-do-you-really-need-semicolon"><a href="#Give-an-example-where-do-you-really-need-semicolon" class="headerlink" title="Give an example where do you really need semicolon"></a>Give an example where do you really need semicolon</h3><p>It is recommended to use semicolons after every statement in JavaScript. For example, in the below case it throws an error “.. is not a function” at runtime due to missing semicolon.</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// define a function</span></span><br><span class="line"><span class="keyword">var</span> fn = <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="comment">//...</span></span><br><span class="line">&#125; <span class="comment">// semicolon missing at this line</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// then execute some code inside a closure</span></span><br><span class="line">(<span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="comment">//...</span></span><br><span class="line">&#125;)();</span><br></pre></td></tr></table></figure><p>and it will be interpreted as</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> fn = <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="comment">//...</span></span><br><span class="line">&#125;(<span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="comment">//...</span></span><br><span class="line">&#125;)();</span><br></pre></td></tr></table></figure><p>In this case, we are passing the second function as an argument to the first function and then trying to call the result of the first function call as a function. Hence, the second function will fail with a “… is not a function” error at runtime.</p><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-a-freeze-method"><a href="#What-is-a-freeze-method" class="headerlink" title="What is a freeze method"></a>What is a freeze method</h3><p>The <strong>freeze()</strong> method is used to freeze an object. Freezing an object does not allow adding new properties to an object,prevents from removing and prevents changing the enumerability, configurability, or writability of existing properties. i.e, It returns the passed object and does not create a frozen copy.</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> obj = &#123;</span><br><span class="line">  prop: <span class="number">100</span></span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="built_in">Object</span>.freeze(obj);</span><br><span class="line">obj.prop = <span class="number">200</span>; <span class="comment">// Throws an error in strict mode</span></span><br><span class="line"></span><br><span class="line"><span class="built_in">console</span>.log(obj.prop); <span class="comment">//100</span></span><br></pre></td></tr></table></figure><p><strong>Note:</strong> It causes a TypeError if the argument passed is not an object.</p><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-the-purpose-of-freeze-method"><a href="#What-is-the-purpose-of-freeze-method" class="headerlink" title="What is the purpose of freeze method"></a>What is the purpose of freeze method</h3><p>Below are the main benefits of using freeze method,</p><ol><li>It is used for freezing objects and arrays.</li><li>It is used to make an object immutable.</li></ol><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="Why-do-I-need-to-use-freeze-method"><a href="#Why-do-I-need-to-use-freeze-method" class="headerlink" title="Why do I need to use freeze method"></a>Why do I need to use freeze method</h3><p>In the Object-oriented paradigm, an existing API contains certain elements that are not intended to be extended, modified, or re-used outside of their current context. Hence it works as the <code>final</code> keyword which is used in various languages.</p><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="How-do-you-detect-a-browser-language-preference"><a href="#How-do-you-detect-a-browser-language-preference" class="headerlink" title="How do you detect a browser language preference"></a>How do you detect a browser language preference</h3><p>You can use navigator object to detect a browser language preference as below,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> language = navigator.languages &amp;&amp; navigator.languages[<span class="number">0</span>] || <span class="comment">// Chrome / Firefox</span></span><br><span class="line">               navigator.language ||   <span class="comment">// All browsers</span></span><br><span class="line">               navigator.userLanguage; <span class="comment">// IE &lt;= 10</span></span><br><span class="line"></span><br><span class="line"><span class="built_in">console</span>.log(language);</span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="How-to-convert-string-to-title-case-with-javascript"><a href="#How-to-convert-string-to-title-case-with-javascript" class="headerlink" title="How to convert string to title case with javascript"></a>How to convert string to title case with javascript</h3><p>Title case means that the first letter of each word is capitalized. You can convert a string to title case using the below function,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">toTitleCase</span>(<span class="params">str</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> str.replace(</span><br><span class="line">        /\w\S*/g,</span><br><span class="line">        <span class="function"><span class="keyword">function</span>(<span class="params">txt</span>) </span>&#123;</span><br><span class="line">            <span class="keyword">return</span> txt.charAt(<span class="number">0</span>).toUpperCase() + txt.substr(<span class="number">1</span>).toLowerCase();</span><br><span class="line">        &#125;</span><br><span class="line">    );</span><br><span class="line">&#125;</span><br><span class="line">toTitleCase(<span class="string">&quot;good morning john&quot;</span>); <span class="comment">// Good Morning John</span></span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="How-do-you-detect-javascript-disabled-in-the-page"><a href="#How-do-you-detect-javascript-disabled-in-the-page" class="headerlink" title="How do you detect javascript disabled in the page"></a>How do you detect javascript disabled in the page</h3><p>You can use the <code>&lt;noscript&gt;</code> tag to detect javascript disabled or not. The code block inside <code>&lt;noscript&gt;</code> gets executed when JavaScript is disabled, and is typically used to display alternative content when the page generated in JavaScript.</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">&lt;script type=<span class="string">&quot;javascript&quot;</span>&gt;</span><br><span class="line">    <span class="comment">// JS related code goes here</span></span><br><span class="line">&lt;/script&gt;</span><br><span class="line">&lt;noscript&gt;</span><br><span class="line">    &lt;a href=<span class="string">&quot;next_page.html?noJS=true&quot;</span>&gt;JavaScript is disabled <span class="keyword">in</span> the page. Please click Next Page&lt;/a&gt;</span><br><span class="line">&lt;/noscript&gt;</span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-are-various-operators-supported-by-javascript"><a href="#What-are-various-operators-supported-by-javascript" class="headerlink" title="What are various operators supported by javascript"></a>What are various operators supported by javascript</h3><p>An operator is capable of manipulating(mathematical and logical computations) a certain value or operand. There are various operators supported by JavaScript as below,</p><ol><li><strong>Arithmetic Operators:</strong> Includes + (Addition),– (Subtraction), * (Multiplication), / (Division), % (Modulus), + + (Increment) and – – (Decrement)</li><li><strong>Comparison Operators:</strong> Includes = =(Equal),!= (Not Equal), ===(Equal with type), &gt; (Greater than),&gt; = (Greater than or Equal to),&lt; (Less than),&lt;= (Less than or Equal to)</li><li><strong>Logical Operators:</strong> Includes &amp;&amp;(Logical AND),||(Logical OR),!(Logical NOT)</li><li><strong>Assignment Operators:</strong> Includes = (Assignment Operator), += (Add and Assignment Operator), – = (Subtract and Assignment Operator), *= (Multiply and Assignment), /= (Divide and Assignment), %= (Modules and Assignment)</li><li><strong>Ternary Operators:</strong> It includes conditional(: ?) Operator</li><li><strong>typeof Operator:</strong> It uses to find type of variable. The syntax looks like <code>typeof variable</code></li></ol><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-a-rest-parameter"><a href="#What-is-a-rest-parameter" class="headerlink" title="What is a rest parameter"></a>What is a rest parameter</h3><p>Rest parameter is an improved way to handle function parameters which allows us to represent an indefinite number of arguments as an array. The syntax would be as below,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">f</span>(<span class="params">a, b, ...theArgs</span>) </span>&#123;</span><br><span class="line">  <span class="comment">// ...</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>For example, let’s take a sum example to calculate on dynamic number of parameters,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">total</span>(<span class="params">…args</span>)</span>&#123;</span><br><span class="line"><span class="keyword">let</span> sum = <span class="number">0</span>;</span><br><span class="line"><span class="keyword">for</span>(<span class="keyword">let</span> i <span class="keyword">of</span> args)&#123;</span><br><span class="line">sum+=i;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">return</span> sum;</span><br><span class="line">&#125;</span><br><span class="line"><span class="built_in">console</span>.log(fun(<span class="number">1</span>,<span class="number">2</span>)); <span class="comment">//3</span></span><br><span class="line"><span class="built_in">console</span>.log(fun(<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>)); <span class="comment">//6</span></span><br><span class="line"><span class="built_in">console</span>.log(fun(<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>)); <span class="comment">//13</span></span><br><span class="line"><span class="built_in">console</span>.log(fun(<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>)); <span class="comment">//15</span></span><br></pre></td></tr></table></figure><p><strong>Note:</strong> Rest parameter is added in ES2015 or ES6</p><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-happens-if-you-do-not-use-rest-parameter-as-a-last-argument"><a href="#What-happens-if-you-do-not-use-rest-parameter-as-a-last-argument" class="headerlink" title="What happens if you do not use rest parameter as a last argument"></a>What happens if you do not use rest parameter as a last argument</h3><p>The rest parameter should be the last argument, as its job is to collect all the remaining arguments into an array. For example, if you define a function like below it doesn’t make any sense and will throw an error.</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">someFunc</span>(<span class="params">a,…b,c</span>)</span>&#123;</span><br><span class="line"><span class="comment">//You code goes here</span></span><br><span class="line"><span class="keyword">return</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-are-the-bitwise-operators-available-in-javascript"><a href="#What-are-the-bitwise-operators-available-in-javascript" class="headerlink" title="What are the bitwise operators available in javascript"></a>What are the bitwise operators available in javascript</h3><p>Below are the list of bitwise logical operators used in JavaScript</p><ol><li>Bitwise AND ( &amp; )</li><li>Bitwise OR ( | )</li><li>Bitwise XOR ( ^ )</li><li>Bitwise NOT ( ~ )</li><li>Left Shift ( &lt;&lt; )</li><li>Sign Propagating Right Shift ( &gt;&gt; )</li><li>Zero fill Right Shift ( &gt;&gt;&gt; )</li></ol><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-a-spread-operator"><a href="#What-is-a-spread-operator" class="headerlink" title="What is a spread operator"></a>What is a spread operator</h3><p>Spread operator allows iterables( arrays / objects / strings ) to be expanded into single arguments/elements. Let’s take an example to see this behavior,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">calculateSum</span>(<span class="params">x, y, z</span>) </span>&#123;</span><br><span class="line">  <span class="keyword">return</span> x + y + z;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> numbers = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>];</span><br><span class="line"></span><br><span class="line"><span class="built_in">console</span>.log(calculateSum(...numbers)); <span class="comment">// 6</span></span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="How-do-you-determine-whether-object-is-frozen-or-not"><a href="#How-do-you-determine-whether-object-is-frozen-or-not" class="headerlink" title="How do you determine whether object is frozen or not"></a>How do you determine whether object is frozen or not</h3><p>Object.isFrozen() method is used to determine if an object is frozen or not.An object is frozen if all of the below conditions hold true,</p><ol><li>If it is not extensible.</li><li>If all of its properties are non-configurable.</li><li>If all its data properties are non-writable.<br>The usage is going to be as follows,</li></ol><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> object = &#123;</span><br><span class="line">   property: <span class="string">&#x27;Welcome JS world&#x27;</span></span><br><span class="line">&#125;;</span><br><span class="line"><span class="built_in">Object</span>.freeze(object);</span><br><span class="line"><span class="built_in">console</span>.log(<span class="built_in">Object</span>.isFrozen(object));</span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="How-do-you-determine-two-values-same-or-not-using-object"><a href="#How-do-you-determine-two-values-same-or-not-using-object" class="headerlink" title="How do you determine two values same or not using object"></a>How do you determine two values same or not using object</h3><p>The Object.is() method determines whether two values are the same value. For example, the usage with different types of values would be,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">Object</span>.is(<span class="string">&#x27;hello&#x27;</span>, <span class="string">&#x27;hello&#x27;</span>);     <span class="comment">// true</span></span><br><span class="line"><span class="built_in">Object</span>.is(<span class="built_in">window</span>, <span class="built_in">window</span>);   <span class="comment">// true</span></span><br><span class="line"><span class="built_in">Object</span>.is([], []) <span class="comment">// false</span></span><br></pre></td></tr></table></figure><p>Two values are the same if one of the following holds:</p><ol><li>both undefined</li><li>both null</li><li>both true or both false</li><li>both strings of the same length with the same characters in the same order</li><li>both the same object (means both object have same reference)</li><li>both numbers and<br>both +0<br>both -0<br>both NaN<br>both non-zero and both not NaN and both have the same value.</li></ol><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-the-purpose-of-using-object-is-method"><a href="#What-is-the-purpose-of-using-object-is-method" class="headerlink" title="What is the purpose of using object is method"></a>What is the purpose of using object is method</h3><p>Some of the applications of Object’s <code>is</code> method are follows,</p><ol><li>It is used for comparison of two strings.</li><li>It is used for comparison of two numbers.</li><li>It is used for comparing the polarity of two numbers.</li><li>It is used for comparison of two objects.</li></ol><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="How-do-you-copy-properties-from-one-object-to-other"><a href="#How-do-you-copy-properties-from-one-object-to-other" class="headerlink" title="How do you copy properties from one object to other"></a>How do you copy properties from one object to other</h3><p>You can use the Object.assign() method which is used to copy the values and properties from one or more source objects to a target object. It returns the target object which has properties and values copied from the target object. The syntax would be as below,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">Object</span>.assign(target, ...sources)</span><br></pre></td></tr></table></figure><p>Let’s take example with one source and one target object,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> target = &#123; <span class="attr">a</span>: <span class="number">1</span>, <span class="attr">b</span>: <span class="number">2</span> &#125;;</span><br><span class="line"><span class="keyword">const</span> source = &#123; <span class="attr">b</span>: <span class="number">3</span>, <span class="attr">c</span>: <span class="number">4</span> &#125;;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> returnedTarget = <span class="built_in">Object</span>.assign(target, source);</span><br><span class="line"></span><br><span class="line"><span class="built_in">console</span>.log(target); <span class="comment">// &#123; a: 1, b: 3, c: 4 &#125;</span></span><br><span class="line"></span><br><span class="line"><span class="built_in">console</span>.log(returnedTarget); <span class="comment">// &#123; a: 1, b: 3, c: 4 &#125;</span></span><br></pre></td></tr></table></figure><p>As observed in the above code, there is a common property(<code>b</code>) from source to target so it’s value has been overwritten.</p><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-are-the-applications-of-assign-method"><a href="#What-are-the-applications-of-assign-method" class="headerlink" title="What are the applications of assign method"></a>What are the applications of assign method</h3><p>Below are the some of main applications of Object.assign() method,</p><ol><li>It is used for cloning an object.</li><li>It is used to merge objects with the same properties.</li></ol><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-a-proxy-object"><a href="#What-is-a-proxy-object" class="headerlink" title="What is a proxy object"></a>What is a proxy object</h3><p>The Proxy object is used to define custom behavior for fundamental operations such as property lookup, assignment, enumeration, function invocation, etc. The syntax would be as follows,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> p = <span class="keyword">new</span> <span class="built_in">Proxy</span>(target, handler);</span><br></pre></td></tr></table></figure><p>Let’s take an example of proxy object,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> handler = &#123;</span><br><span class="line">    get: <span class="function"><span class="keyword">function</span>(<span class="params">obj, prop</span>) </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> prop <span class="keyword">in</span> obj ?</span><br><span class="line">            obj[prop] :</span><br><span class="line">            <span class="number">100</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> p = <span class="keyword">new</span> <span class="built_in">Proxy</span>(&#123;&#125;, handler);</span><br><span class="line">p.a = <span class="number">10</span>;</span><br><span class="line">p.b = <span class="literal">null</span>;</span><br><span class="line"></span><br><span class="line"><span class="built_in">console</span>.log(p.a, p.b); <span class="comment">// 10, null</span></span><br><span class="line"><span class="built_in">console</span>.log(<span class="string">&#x27;c&#x27;</span> <span class="keyword">in</span> p, p.c); <span class="comment">// false, 100</span></span><br></pre></td></tr></table></figure><p>In the above code, it uses <code>get</code> handler which define the behavior of the proxy when an operation is performed on it</p><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-the-purpose-of-seal-method"><a href="#What-is-the-purpose-of-seal-method" class="headerlink" title="What is the purpose of seal method"></a>What is the purpose of seal method</h3><p>The <strong>Object.seal()</strong> method is used to seal an object, by preventing new properties from being added to it and marking all existing properties as non-configurable. But values of present properties can still be changed as long as they are writable. Let’s see the below example to understand more about seal() method</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> object = &#123;</span><br><span class="line">   property: <span class="string">&#x27;Welcome JS world&#x27;</span></span><br><span class="line">&#125;;</span><br><span class="line"><span class="built_in">Object</span>.seal(object);</span><br><span class="line">object.property = <span class="string">&#x27;Welcome to object world&#x27;</span>;</span><br><span class="line"><span class="built_in">console</span>.log(<span class="built_in">Object</span>.isSealed(object)); <span class="comment">// true</span></span><br><span class="line"><span class="keyword">delete</span> object.property; <span class="comment">// You cannot delete when sealed</span></span><br><span class="line"><span class="built_in">console</span>.log(object.property); <span class="comment">//Welcome to object world</span></span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-are-the-applications-of-seal-method"><a href="#What-are-the-applications-of-seal-method" class="headerlink" title="What are the applications of seal method"></a>What are the applications of seal method</h3><p>Below are the main applications of Object.seal() method,</p><ol><li>It is used for sealing objects and arrays.</li><li>It is used to make an object immutable.</li></ol><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-are-the-differences-between-freeze-and-seal-methods"><a href="#What-are-the-differences-between-freeze-and-seal-methods" class="headerlink" title="What are the differences between freeze and seal methods"></a>What are the differences between freeze and seal methods</h3><p>If an object is frozen using the Object.freeze() method then its properties become immutable and no changes can be made in them whereas if an object is sealed using the Object.seal() method then the changes can be made in the existing properties of the object.</p><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="How-do-you-determine-if-an-object-is-sealed-or-not"><a href="#How-do-you-determine-if-an-object-is-sealed-or-not" class="headerlink" title="How do you determine if an object is sealed or not"></a>How do you determine if an object is sealed or not</h3><p>The Object.isSealed() method is used to determine if an object is sealed or not. An object is sealed if all of the below conditions hold true</p><ol><li>If it is not extensible.</li><li>If all of its properties are non-configurable.</li><li>If it is not removable (but not necessarily non-writable).<br>Let’s see it in the action</li></ol><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> object = &#123;</span><br><span class="line">property: <span class="string">&#x27;Hello, Good morning&#x27;</span></span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="built_in">Object</span>.seal(object); <span class="comment">// Using seal() method to seal the object</span></span><br><span class="line"></span><br><span class="line"><span class="built_in">console</span>.log(<span class="built_in">Object</span>.isSealed(object));      <span class="comment">// checking whether the object is sealed or not</span></span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="How-do-you-get-enumerable-key-and-value-pairs"><a href="#How-do-you-get-enumerable-key-and-value-pairs" class="headerlink" title="How do you get enumerable key and value pairs"></a>How do you get enumerable key and value pairs</h3><p>The Object.entries() method is used to return an array of a given object’s own enumerable string-keyed property [key, value] pairs, in the same order as that provided by a for…in loop. Let’s see the functionality of object.entries() method in an example,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> object = &#123;</span><br><span class="line">  a: <span class="string">&#x27;Good morning&#x27;</span>,</span><br><span class="line">  b: <span class="number">100</span></span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="keyword">for</span> (<span class="keyword">let</span> [key, value] <span class="keyword">of</span> <span class="built_in">Object</span>.entries(object)) &#123;</span><br><span class="line">  <span class="built_in">console</span>.log(<span class="string">`<span class="subst">$&#123;key&#125;</span>: <span class="subst">$&#123;value&#125;</span>`</span>); <span class="comment">// a: &#x27;Good morning&#x27;</span></span><br><span class="line">                                   <span class="comment">// b: 100</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p><strong>Note:</strong> The order is not guaranteed as object defined.</p><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-the-main-difference-between-Object-values-and-Object-entries-method"><a href="#What-is-the-main-difference-between-Object-values-and-Object-entries-method" class="headerlink" title="What is the main difference between Object.values and Object.entries method"></a>What is the main difference between Object.values and Object.entries method</h3><p>The Object.values() method’s behavior is similar to Object.entries() method but it returns an array of values instead [key,value] pairs.</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> object = &#123;</span><br><span class="line">  a: <span class="string">&#x27;Good morning&#x27;</span>,</span><br><span class="line">  b: <span class="number">100</span></span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="keyword">for</span> (<span class="keyword">let</span> value <span class="keyword">of</span> <span class="built_in">Object</span>.values(object)) &#123;</span><br><span class="line">  <span class="built_in">console</span>.log(<span class="string">`<span class="subst">$&#123;value&#125;</span>`</span>); <span class="comment">// &#x27;Good morning&#x27;</span></span><br><span class="line">                               <span class="number">100</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="How-can-you-get-the-list-of-keys-of-any-object"><a href="#How-can-you-get-the-list-of-keys-of-any-object" class="headerlink" title="How can you get the list of keys of any object"></a>How can you get the list of keys of any object</h3><p>You can use the <code>Object.keys()</code> method which is used to return an array of a given object’s own property names, in the same order as we get with a normal loop. For example, you can get the keys of a user object,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> user = &#123;</span><br><span class="line">  name: <span class="string">&#x27;John&#x27;</span>,</span><br><span class="line">  gender: <span class="string">&#x27;male&#x27;</span>,</span><br><span class="line">  age: <span class="number">40</span></span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="built_in">console</span>.log(<span class="built_in">Object</span>.keys(user)); <span class="comment">//[&#x27;name&#x27;, &#x27;gender&#x27;, &#x27;age&#x27;]</span></span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="How-do-you-create-an-object-with-prototype"><a href="#How-do-you-create-an-object-with-prototype" class="headerlink" title="How do you create an object with prototype"></a>How do you create an object with prototype</h3><p>The Object.create() method is used to create a new object with the specified prototype object and properties. i.e, It uses an existing object as the prototype of the newly created object. It returns a new object with the specified prototype object and properties.</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> user = &#123;</span><br><span class="line">  name: <span class="string">&#x27;John&#x27;</span>,</span><br><span class="line">  printInfo: <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">`My name is <span class="subst">$&#123;<span class="built_in">this</span>.name&#125;</span>.`</span>);</span><br><span class="line">  &#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> admin = <span class="built_in">Object</span>.create(user);</span><br><span class="line"></span><br><span class="line">admin.name = <span class="string">&quot;Nick&quot;</span>; <span class="comment">// Remember that &quot;name&quot; is a property set on &quot;admin&quot; but not on &quot;user&quot; object</span></span><br><span class="line"></span><br><span class="line">admin.printInfo(); <span class="comment">// My name is Nick</span></span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-a-WeakSet"><a href="#What-is-a-WeakSet" class="headerlink" title="What is a WeakSet"></a>What is a WeakSet</h3><p>WeakSet is used to store a collection of weakly(weak references) held objects. The syntax would be as follows,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">new</span> <span class="built_in">WeakSet</span>([iterable]);</span><br></pre></td></tr></table></figure><p>Let’s see the below example to explain it’s behavior,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> ws = <span class="keyword">new</span> <span class="built_in">WeakSet</span>();</span><br><span class="line"><span class="keyword">var</span> user = &#123;&#125;;</span><br><span class="line">ws.add(user);</span><br><span class="line">ws.has(user);    <span class="comment">// true</span></span><br><span class="line">ws.delete(user); <span class="comment">// removes user from the set</span></span><br><span class="line">ws.has(user);    <span class="comment">// false, user has been removed</span></span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-are-the-differences-between-WeakSet-and-Set"><a href="#What-are-the-differences-between-WeakSet-and-Set" class="headerlink" title="What are the differences between WeakSet and Set"></a>What are the differences between WeakSet and Set</h3><p>The main difference is that references to objects in Set are strong while references to objects in WeakSet are weak. i.e, An object in WeakSet can be garbage collected if there is no other reference to it.<br>Other differences are,</p><ol><li>Sets can store any value Whereas WeakSets can store only collections of objects</li><li>WeakSet does not have size property unlike Set</li><li>WeakSet does not have methods such as clear, keys, values, entries, forEach.</li><li>WeakSet is not iterable.</li></ol><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="List-down-the-collection-of-methods-available-on-WeakSet"><a href="#List-down-the-collection-of-methods-available-on-WeakSet" class="headerlink" title="List down the collection of methods available on WeakSet"></a>List down the collection of methods available on WeakSet</h3><p>Below are the list of methods available on WeakSet,</p><ol><li>add(value): A new object is appended with the given value to the weakset</li><li>delete(value): Deletes the value from the WeakSet collection.</li><li>has(value): It returns true if the value is present in the WeakSet Collection, otherwise it returns false.</li><li>length(): It returns the length of weakSetObject<br>Let’s see the functionality of all the above methods in an example,</li></ol><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> weakSetObject = <span class="keyword">new</span> <span class="built_in">WeakSet</span>();</span><br><span class="line"><span class="keyword">var</span> firstObject = &#123;&#125;;</span><br><span class="line"><span class="keyword">var</span> secondObject = &#123;&#125;;</span><br><span class="line"><span class="comment">// add(value)</span></span><br><span class="line">weakSetObject.add(firstObject);</span><br><span class="line">weakSetObject.add(secondObject);</span><br><span class="line"><span class="built_in">console</span>.log(weakSetObject.has(firstObject)); <span class="comment">//true</span></span><br><span class="line"><span class="built_in">console</span>.log(weakSetObject.length()); <span class="comment">//2</span></span><br><span class="line">weakSetObject.delete(secondObject);</span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-a-WeakMap"><a href="#What-is-a-WeakMap" class="headerlink" title="What is a WeakMap"></a>What is a WeakMap</h3><p>The WeakMap object is a collection of key/value pairs in which the keys are weakly referenced. In this case, keys must be objects and the values can be arbitrary values. The syntax is looking like as below,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">new</span> <span class="built_in">WeakMap</span>([iterable])</span><br></pre></td></tr></table></figure><p>Let’s see the below example to explain it’s behavior,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> ws = <span class="keyword">new</span> <span class="built_in">WeakMap</span>();</span><br><span class="line"><span class="keyword">var</span> user = &#123;&#125;;</span><br><span class="line">ws.set(user);</span><br><span class="line">ws.has(user);    <span class="comment">// true</span></span><br><span class="line">ws.delete(user); <span class="comment">// removes user from the map</span></span><br><span class="line">ws.has(user);    <span class="comment">// false, user has been removed</span></span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-are-the-differences-between-WeakMap-and-Map"><a href="#What-are-the-differences-between-WeakMap-and-Map" class="headerlink" title="What are the differences between WeakMap and Map"></a>What are the differences between WeakMap and Map</h3><p>The main difference is that references to key objects in Map are strong while references to key objects in WeakMap are weak. i.e, A key object in WeakMap can be garbage collected if there is no other reference to it.<br>Other differences are,</p><ol><li>Maps can store any key type Whereas WeakMaps can store only collections of key objects</li><li>WeakMap does not have size property unlike Map</li><li>WeakMap does not have methods such as clear, keys, values, entries, forEach.</li><li>WeakMap is not iterable.</li></ol><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="List-down-the-collection-of-methods-available-on-WeakMap"><a href="#List-down-the-collection-of-methods-available-on-WeakMap" class="headerlink" title="List down the collection of methods available on WeakMap"></a>List down the collection of methods available on WeakMap</h3><p>Below are the list of methods available on WeakMap,</p><ol><li>set(key, value): Sets the value for the key in the WeakMap object. Returns the WeakMap object.</li><li>delete(key): Removes any value associated to the key.</li><li>has(key): Returns a Boolean asserting whether a value has been associated to the key in the WeakMap object or not.</li><li>get(key): Returns the value associated to the key, or undefined if there is none.<br>Let’s see the functionality of all the above methods in an example,</li></ol><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> weakMapObject = <span class="keyword">new</span> <span class="built_in">WeakMap</span>();</span><br><span class="line"><span class="keyword">var</span> firstObject = &#123;&#125;;</span><br><span class="line"><span class="keyword">var</span> secondObject = &#123;&#125;;</span><br><span class="line"><span class="comment">// set(key, value)</span></span><br><span class="line">weakMapObject.set(firstObject, <span class="string">&#x27;John&#x27;</span>);</span><br><span class="line">weakMapObject.set(secondObject, <span class="number">100</span>);</span><br><span class="line"><span class="built_in">console</span>.log(weakMapObject.has(firstObject)); <span class="comment">//true</span></span><br><span class="line"><span class="built_in">console</span>.log(weakMapObject.get(firstObject)); <span class="comment">// John</span></span><br><span class="line">weakMapObject.delete(secondObject);</span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-the-purpose-of-uneval"><a href="#What-is-the-purpose-of-uneval" class="headerlink" title="What is the purpose of uneval"></a>What is the purpose of uneval</h3><p>The uneval() is an inbuilt function which is used to create a string representation of the source code of an Object. It is a top-level function and is not associated with any object. Let’s see the below example to know more about it’s functionality,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> a = <span class="number">1</span>;</span><br><span class="line">uneval(a); <span class="comment">// returns a String containing 1</span></span><br><span class="line">uneval(<span class="function"><span class="keyword">function</span> <span class="title">user</span>(<span class="params"></span>) </span>&#123;&#125;); <span class="comment">// returns &quot;(function user()&#123;&#125;)&quot;</span></span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="How-do-you-encode-an-URL"><a href="#How-do-you-encode-an-URL" class="headerlink" title="How do you encode an URL"></a>How do you encode an URL</h3><p>The encodeURI() function is used to encode complete URI which has special characters except (, / ? : @ &amp; = + $ #) characters.</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> uri = <span class="string">&#x27;https://mozilla.org/?x=шеллы&#x27;</span>;</span><br><span class="line"><span class="keyword">var</span> encoded = <span class="built_in">encodeURI</span>(uri);</span><br><span class="line"><span class="built_in">console</span>.log(encoded); <span class="comment">// https://mozilla.org/?x=%D1%88%D0%B5%D0%BB%D0%BB%D1%8B</span></span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="How-do-you-decode-an-URL"><a href="#How-do-you-decode-an-URL" class="headerlink" title="How do you decode an URL"></a>How do you decode an URL</h3><p>The decodeURI() function is used to decode a Uniform Resource Identifier (URI) previously created by encodeURI().</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"> <span class="keyword">var</span> uri = <span class="string">&#x27;https://mozilla.org/?x=шеллы&#x27;</span>;</span><br><span class="line"> <span class="keyword">var</span> encoded = <span class="built_in">encodeURI</span>(uri);</span><br><span class="line"> <span class="built_in">console</span>.log(encoded); <span class="comment">// https://mozilla.org/?x=%D1%88%D0%B5%D0%BB%D0%BB%D1%8B</span></span><br><span class="line"><span class="keyword">try</span> &#123;</span><br><span class="line">  <span class="built_in">console</span>.log(<span class="built_in">decodeURI</span>(encoded)); <span class="comment">// &quot;https://mozilla.org/?x=шеллы&quot;</span></span><br><span class="line">&#125; <span class="keyword">catch</span>(e) &#123; <span class="comment">// catches a malformed URI</span></span><br><span class="line">  <span class="built_in">console</span>.error(e);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="How-do-you-print-the-contents-of-web-page"><a href="#How-do-you-print-the-contents-of-web-page" class="headerlink" title="How do you print the contents of web page"></a>How do you print the contents of web page</h3><p>The window object provided a print() method which is used to print the contents of the current window. It opens a Print dialog box which lets you choose between various printing options. Let’s see the usage of print method in an example,</p><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">&quot;button&quot;</span> <span class="attr">value</span>=<span class="string">&quot;Print&quot;</span> <span class="attr">onclick</span>=<span class="string">&quot;window.print()&quot;</span> /&gt;</span></span><br></pre></td></tr></table></figure><p><strong>Note:</strong> In most browsers, it will block while the print dialog is open.</p><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-the-difference-between-uneval-and-eval"><a href="#What-is-the-difference-between-uneval-and-eval" class="headerlink" title="What is the difference between uneval and eval"></a>What is the difference between uneval and eval</h3><p>The <code>uneval</code> function returns the source of a given object; whereas the <code>eval</code> function does the opposite, by evaluating that source code in a different memory area. Let’s see an example to clarify the difference,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> msg = uneval(<span class="function"><span class="keyword">function</span> <span class="title">greeting</span>(<span class="params"></span>) </span>&#123; <span class="keyword">return</span> <span class="string">&#x27;Hello, Good morning&#x27;</span>; &#125;);</span><br><span class="line"><span class="keyword">var</span> greeting = <span class="built_in">eval</span>(msg);</span><br><span class="line">greeting(); <span class="comment">// returns &quot;Hello, Good morning&quot;</span></span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-an-anonymous-function"><a href="#What-is-an-anonymous-function" class="headerlink" title="What is an anonymous function"></a>What is an anonymous function</h3><p>An anonymous function is a function without a name! Anonymous functions are commonly assigned to a variable name or used as a callback function. The syntax would be as below,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> (<span class="params">optionalParameters</span>) </span>&#123;</span><br><span class="line">  <span class="comment">//do something</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> myFunction = <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123; <span class="comment">//Anonymous function assigned to a variable</span></span><br><span class="line">  <span class="comment">//do something</span></span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line">[<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>].map(<span class="function"><span class="keyword">function</span>(<span class="params">element</span>)</span>&#123; <span class="comment">//Anonymous function used as a callback function</span></span><br><span class="line">  <span class="comment">//do something</span></span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure><p>Let’s see the above anonymous function in an example,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> x = <span class="function"><span class="keyword">function</span> (<span class="params">a, b</span>) </span>&#123;<span class="keyword">return</span> a * b&#125;;</span><br><span class="line"><span class="keyword">var</span> z = x(<span class="number">5</span>, <span class="number">10</span>);</span><br><span class="line"><span class="built_in">console</span>.log(z); <span class="comment">// 50</span></span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-the-precedence-order-between-local-and-global-variables"><a href="#What-is-the-precedence-order-between-local-and-global-variables" class="headerlink" title="What is the precedence order between local and global variables"></a>What is the precedence order between local and global variables</h3><p>A local variable takes precedence over a global variable with the same name. Let’s see this behavior in an example.</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> msg = <span class="string">&quot;Good morning&quot;</span>;</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">greeting</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">   msg = <span class="string">&quot;Good Evening&quot;</span>;</span><br><span class="line">   <span class="built_in">console</span>.log(msg);</span><br><span class="line">&#125;</span><br><span class="line">greeting();</span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-are-javascript-accessors"><a href="#What-are-javascript-accessors" class="headerlink" title="What are javascript accessors"></a>What are javascript accessors</h3><p>ECMAScript 5 introduced javascript object accessors or computed properties through getters and setters. Getters uses the <code>get</code> keyword whereas Setters uses the <code>set</code> keyword.</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> user = &#123;</span><br><span class="line">  firstName: <span class="string">&quot;John&quot;</span>,</span><br><span class="line">  lastName : <span class="string">&quot;Abraham&quot;</span>,</span><br><span class="line">  language : <span class="string">&quot;en&quot;</span>,</span><br><span class="line">  <span class="keyword">get</span> <span class="title">lang</span>() &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="built_in">this</span>.language;</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="keyword">set</span> <span class="title">lang</span>(<span class="params">lang</span>) &#123;</span><br><span class="line">  <span class="built_in">this</span>.language = lang;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;;</span><br><span class="line"><span class="built_in">console</span>.log(user.lang); <span class="comment">// getter access lang as en</span></span><br><span class="line">user.lang = <span class="string">&#x27;fr&#x27;</span>;</span><br><span class="line"><span class="built_in">console</span>.log(user.lang); <span class="comment">// setter used to set lang as fr</span></span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="How-do-you-define-property-on-Object-constructor"><a href="#How-do-you-define-property-on-Object-constructor" class="headerlink" title="How do you define property on Object constructor"></a>How do you define property on Object constructor</h3><p>The Object.defineProperty() static method is used to define a new property directly on an object, or modify an existing property on an object, and returns the object. Let’s see an example to know how to define property,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> newObject = &#123;&#125;;</span><br><span class="line"></span><br><span class="line"><span class="built_in">Object</span>.defineProperty(newObject, <span class="string">&#x27;newProperty&#x27;</span>, &#123;</span><br><span class="line">  value: <span class="number">100</span>,</span><br><span class="line">  writable: <span class="literal">false</span></span><br><span class="line">&#125;);</span><br><span class="line"></span><br><span class="line"><span class="built_in">console</span>.log(newObject.newProperty); <span class="comment">// 100</span></span><br><span class="line"></span><br><span class="line">newObject.newProperty = <span class="number">200</span>; <span class="comment">// It throws an error in strict mode due to writable setting</span></span><br><span class="line"></span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-the-difference-between-get-and-defineProperty"><a href="#What-is-the-difference-between-get-and-defineProperty" class="headerlink" title="What is the difference between get and defineProperty"></a>What is the difference between get and defineProperty</h3><p>Both have similar results until unless you use classes. If you use <code>get</code> the property will be defined on the prototype of the object whereas using <code>Object.defineProperty()</code> the property will be defined on the instance it is applied to.</p><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-are-the-advantages-of-Getters-and-Setters"><a href="#What-are-the-advantages-of-Getters-and-Setters" class="headerlink" title="What are the advantages of Getters and Setters"></a>What are the advantages of Getters and Setters</h3><p>Below are the list of benefits of Getters and Setters,</p><ol><li>They provide simpler syntax</li><li>They are used for defining computed properties, or accessors in JS.</li><li>Useful to provide equivalence relation between properties and methods</li><li>They can provide better data quality</li><li>Useful for doing things behind the scenes with the encapsulated logic.</li></ol><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="Can-I-add-getters-and-setters-using-defineProperty-method"><a href="#Can-I-add-getters-and-setters-using-defineProperty-method" class="headerlink" title="Can I add getters and setters using defineProperty method"></a>Can I add getters and setters using defineProperty method</h3><p>Yes, You can use the <code>Object.defineProperty()</code> method to add Getters and Setters. For example, the below counter object uses increment, decrement, add and subtract properties,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> obj = &#123;<span class="attr">counter</span> : <span class="number">0</span>&#125;;</span><br><span class="line"></span><br><span class="line"><span class="comment">// Define getters</span></span><br><span class="line"><span class="built_in">Object</span>.defineProperty(obj, <span class="string">&quot;increment&quot;</span>, &#123;</span><br><span class="line">  get : <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;<span class="built_in">this</span>.counter++;&#125;</span><br><span class="line">&#125;);</span><br><span class="line"><span class="built_in">Object</span>.defineProperty(obj, <span class="string">&quot;decrement&quot;</span>, &#123;</span><br><span class="line">  get : <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;<span class="built_in">this</span>.counter--;&#125;</span><br><span class="line">&#125;);</span><br><span class="line"></span><br><span class="line"><span class="comment">// Define setters</span></span><br><span class="line"><span class="built_in">Object</span>.defineProperty(obj, <span class="string">&quot;add&quot;</span>, &#123;</span><br><span class="line">  set : <span class="function"><span class="keyword">function</span> (<span class="params">value</span>) </span>&#123;<span class="built_in">this</span>.counter += value;&#125;</span><br><span class="line">&#125;);</span><br><span class="line"><span class="built_in">Object</span>.defineProperty(obj, <span class="string">&quot;subtract&quot;</span>, &#123;</span><br><span class="line">  set : <span class="function"><span class="keyword">function</span> (<span class="params">value</span>) </span>&#123;<span class="built_in">this</span>.counter -= value;&#125;</span><br><span class="line">&#125;);</span><br><span class="line"></span><br><span class="line">obj.add = <span class="number">10</span>;</span><br><span class="line">obj.subtract = <span class="number">5</span>;</span><br><span class="line"><span class="built_in">console</span>.log(obj.increment); <span class="comment">//6</span></span><br><span class="line"><span class="built_in">console</span>.log(obj.decrement); <span class="comment">//5</span></span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-the-purpose-of-switch-case"><a href="#What-is-the-purpose-of-switch-case" class="headerlink" title="What is the purpose of switch-case"></a>What is the purpose of switch-case</h3><p>The switch case statement in JavaScript is used for decision making purposes. In a few cases, using the switch case statement is going to be more convenient than if-else statements. The syntax would be as below,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">switch</span> (expression)</span><br><span class="line">&#123;</span><br><span class="line">    <span class="keyword">case</span> value1:</span><br><span class="line">        statement1;</span><br><span class="line">        <span class="keyword">break</span>;</span><br><span class="line">    <span class="keyword">case</span> value2:</span><br><span class="line">        statement2;</span><br><span class="line">        <span class="keyword">break</span>;</span><br><span class="line">    .</span><br><span class="line">    .</span><br><span class="line">    <span class="keyword">case</span> valueN:</span><br><span class="line">        statementN;</span><br><span class="line">        <span class="keyword">break</span>;</span><br><span class="line">    <span class="keyword">default</span>:</span><br><span class="line">        statementDefault;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>The above multi-way branch statement provides an easy way to dispatch execution to different parts of code based on the value of the expression.</p><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-are-the-conventions-to-be-followed-for-the-usage-of-switch-case"><a href="#What-are-the-conventions-to-be-followed-for-the-usage-of-switch-case" class="headerlink" title="What are the conventions to be followed for the usage of switch case"></a>What are the conventions to be followed for the usage of switch case</h3><p>Below are the list of conventions should be taken care,</p><ol><li>The expression can be of type either number or string.</li><li>Duplicate values are not allowed for the expression.</li><li>The default statement is optional. If the expression passed to switch does not match with any case value then the statement within default case will be executed.</li><li>The break statement is used inside the switch to terminate a statement sequence.</li><li>The break statement is optional. But if it is omitted, the execution will continue on into the next case.</li></ol><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-are-primitive-data-types"><a href="#What-are-primitive-data-types" class="headerlink" title="What are primitive data types"></a>What are primitive data types</h3><p>A primitive data type is data that has a primitive value (which has no properties or methods). There are 5 types of primitive data types.</p><ol><li>string</li><li>number</li><li>boolean</li><li>null</li><li>undefined</li></ol><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-are-the-different-ways-to-access-object-properties"><a href="#What-are-the-different-ways-to-access-object-properties" class="headerlink" title="What are the different ways to access object properties"></a>What are the different ways to access object properties</h3><p>There are 3 possible ways for accessing the property of an object.</p><ol><li><strong>Dot notation:</strong> It uses dot for accessing the properties</li></ol><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">objectName.property</span><br></pre></td></tr></table></figure><ol><li><strong>Square brackets notation:</strong> It uses square brackets for property access</li></ol><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">objectName[<span class="string">&quot;property&quot;</span>]</span><br></pre></td></tr></table></figure><ol><li><strong>Expression notation:</strong> It uses expression in the square brackets</li></ol><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">objectName[expression]</span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-are-the-function-parameter-rules"><a href="#What-are-the-function-parameter-rules" class="headerlink" title="What are the function parameter rules"></a>What are the function parameter rules</h3><p>JavaScript functions follow below rules for parameters,</p><ol><li>The function definitions do not specify data types for parameters.</li><li>Do not perform type checking on the passed arguments.</li><li>Do not check the number of arguments received.<br>i.e, The below function follows the above rules,</li></ol><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">functionName</span>(<span class="params">parameter1, parameter2, parameter3</span>) </span>&#123;</span><br><span class="line">  <span class="built_in">console</span>.log(parameter1); <span class="comment">// 1</span></span><br><span class="line">&#125;</span><br><span class="line">functionName(<span class="number">1</span>);</span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-an-error-object"><a href="#What-is-an-error-object" class="headerlink" title="What is an error object"></a>What is an error object</h3><p>An error object is a built in error object that provides error information when an error occurs. It has two properties: name and message. For example, the below function logs error details,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">try</span> &#123;</span><br><span class="line">  greeting(<span class="string">&quot;Welcome&quot;</span>);</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">catch</span>(err) &#123;</span><br><span class="line">  <span class="built_in">console</span>.log(err.name + <span class="string">&quot;&lt;br&gt;&quot;</span> + err.message);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="When-you-get-a-syntax-error"><a href="#When-you-get-a-syntax-error" class="headerlink" title="When you get a syntax error"></a>When you get a syntax error</h3><p>A SyntaxError is thrown if you try to evaluate code with a syntax error. For example, the below missing quote for the function parameter throws a syntax error</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">try</span> &#123;</span><br><span class="line">  <span class="built_in">eval</span>(<span class="string">&quot;greeting(&#x27;welcome)&quot;</span>);   <span class="comment">// Missing &#x27; will produce an error</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">catch</span>(err) &#123;</span><br><span class="line">  <span class="built_in">console</span>.log(err.name);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-are-the-different-error-names-from-error-object"><a href="#What-are-the-different-error-names-from-error-object" class="headerlink" title="What are the different error names from error object"></a>What are the different error names from error object</h3><p>There are 6 different types of error names returned from error object,</p><table><thead><tr><th>Error Name</th><th>Description</th></tr></thead><tbody><tr><td>EvalError</td><td>An error has occurred in the eval() function</td></tr><tr><td>RangeError</td><td>An error has occurred with a number “out of range”</td></tr><tr><td>ReferenceError</td><td>An error due to an illegal reference</td></tr><tr><td>SyntaxError</td><td>An error due to a syntax error</td></tr><tr><td>TypeError</td><td>An error due to a type error</td></tr><tr><td>URIError</td><td>An error due to encodeURI()</td></tr></tbody></table><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-are-the-various-statements-in-error-handling"><a href="#What-are-the-various-statements-in-error-handling" class="headerlink" title="What are the various statements in error handling"></a>What are the various statements in error handling</h3><p>Below are the list of statements used in an error handling,</p><ol><li><strong>try:</strong> This statement is used to test a block of code for errors</li><li><strong>catch:</strong> This statement is used to handle the error</li><li><strong>throw:</strong> This statement is used to create custom errors.</li><li><strong>finally:</strong> This statement is used to execute code after try and catch regardless of the result.</li></ol><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-are-the-two-types-of-loops-in-javascript"><a href="#What-are-the-two-types-of-loops-in-javascript" class="headerlink" title="What are the two types of loops in javascript"></a>What are the two types of loops in javascript</h3><ol><li><strong>Entry Controlled loops:</strong> In this kind of loop type, the test condition is tested before entering the loop body. For example, For Loop and While Loop comes under this category.</li><li><strong>Exit Controlled Loops:</strong> In this kind of loop type, the test condition is tested or evaluated at the end of the loop body. i.e, the loop body will execute at least once irrespective of test condition true or false. For example, do-while loop comes under this category.</li></ol><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-nodejs"><a href="#What-is-nodejs" class="headerlink" title="What is nodejs"></a>What is nodejs</h3><p>Node.js is a server-side platform built on Chrome’s JavaScript runtime for easily building fast and scalable network applications. It is an event-based, non-blocking, asynchronous I/O runtime that uses Google’s V8 JavaScript engine and libuv library.</p><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-an-Intl-object"><a href="#What-is-an-Intl-object" class="headerlink" title="What is an Intl object"></a>What is an Intl object</h3><p>The Intl object is the namespace for the ECMAScript Internationalization API, which provides language sensitive string comparison, number formatting, and date and time formatting. It provides access to several constructors and language sensitive functions.</p><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="How-do-you-perform-language-specific-date-and-time-formatting"><a href="#How-do-you-perform-language-specific-date-and-time-formatting" class="headerlink" title="How do you perform language specific date and time formatting"></a>How do you perform language specific date and time formatting</h3><p>You can use the <code>Intl.DateTimeFormat</code> object which is a constructor for objects that enable language-sensitive date and time formatting. Let’s see this behavior with an example,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> date = <span class="keyword">new</span> <span class="built_in">Date</span>(<span class="built_in">Date</span>.UTC(<span class="number">2019</span>, <span class="number">07</span>, <span class="number">07</span>, <span class="number">3</span>, <span class="number">0</span>, <span class="number">0</span>));</span><br><span class="line"><span class="built_in">console</span>.log(<span class="keyword">new</span> <span class="built_in">Intl</span>.DateTimeFormat(<span class="string">&#x27;en-GB&#x27;</span>).format(date)); <span class="comment">// 07/08/2019</span></span><br><span class="line"><span class="built_in">console</span>.log(<span class="keyword">new</span> <span class="built_in">Intl</span>.DateTimeFormat(<span class="string">&#x27;en-AU&#x27;</span>).format(date)); <span class="comment">// 07/08/2019</span></span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-an-Iterator"><a href="#What-is-an-Iterator" class="headerlink" title="What is an Iterator"></a>What is an Iterator</h3><p>An iterator is an object which defines a sequence and a return value upon its termination. It implements the Iterator protocol with a <code>next()</code> method which returns an object with two properties: <code>value</code> (the next value in the sequence) and <code>done</code> (which is true if the last value in the sequence has been consumed).</p><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="How-does-synchronous-iteration-works"><a href="#How-does-synchronous-iteration-works" class="headerlink" title="How does synchronous iteration works"></a>How does synchronous iteration works</h3><p>Synchronous iteration was introduced in ES6 and it works with below set of components,</p><p><strong>Iterable:</strong> It is an object which can be iterated over via a method whose key is Symbol.iterator.<br><strong>Iterator:</strong> It is an object returned by invoking <code>[Symbol.iterator]()</code> on an iterable. This iterator object wraps each iterated element in an object and returns it via <code>next()</code> method one by one.<br><strong>IteratorResult:</strong> It is an object returned by <code>next()</code> method. The object contains two properties; the <code>value</code> property contains an iterated element and the <code>done</code> property determines whether the element is the last element or not.</p><p>Let’s demonstrate synchronous iteration with an array as below,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> iterable = [<span class="string">&#x27;one&#x27;</span>, <span class="string">&#x27;two&#x27;</span>, <span class="string">&#x27;three&#x27;</span>];</span><br><span class="line"><span class="keyword">const</span> iterator = iterable[<span class="built_in">Symbol</span>.iterator]();</span><br><span class="line"><span class="built_in">console</span>.log(iterator.next());  <span class="comment">// &#123; value: &#x27;one&#x27;, done: false &#125;</span></span><br><span class="line"><span class="built_in">console</span>.log(iterator.next());  <span class="comment">// &#123; value: &#x27;two&#x27;, done: false &#125;</span></span><br><span class="line"><span class="built_in">console</span>.log(iterator.next());  <span class="comment">// &#123; value: &#x27;three&#x27;, done: false &#125;</span></span><br><span class="line"><span class="built_in">console</span>.log(iterator.next());  <span class="comment">// &#123; value: &#x27;undefined, done: true &#125;</span></span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-an-event-loop"><a href="#What-is-an-event-loop" class="headerlink" title="What is an event loop"></a>What is an event loop</h3><p>The Event Loop is a queue of callback functions. When an async function executes, the callback function is pushed into the queue. The JavaScript engine doesn’t start processing the event loop until the async function has finished executing the code.<br><strong>Note:</strong> It allows Node.js to perform non-blocking I/O operations even though JavaScript is single-threaded.</p><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-call-stack"><a href="#What-is-call-stack" class="headerlink" title="What is call stack"></a>What is call stack</h3><p>Call Stack is a data structure for javascript interpreters to keep track of function calls in the program. It has two major actions,</p><ol><li>Whenever you call a function for its execution, you are pushing it to the stack.</li><li>Whenever the execution is completed, the function is popped out of the stack.</li></ol><p>Let’s take an example and it’s state representation in a diagram format</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">hungry</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">   eatFruits();</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">eatFruits</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">   <span class="keyword">return</span> <span class="string">&quot;I&#x27;m eating fruits&quot;</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// Invoke the `hungry` function</span></span><br><span class="line">hungry();</span><br></pre></td></tr></table></figure><p>The above code processed in a call stack as below,</p><ol><li>Add the <code>hungry()</code> function to the call stack list and execute the code.</li><li>Add the <code>eatFruits()</code> function to the call stack list and execute the code.</li><li>Delete the <code>eatFruits()</code> function from our call stack list.</li><li>Delete the <code>hungry()</code> function from the call stack list since there are no items anymore.</li></ol><p><img src="/images/call-stack.png" alt="Screenshot"></p><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-an-event-queue"><a href="#What-is-an-event-queue" class="headerlink" title="What is an event queue"></a>What is an event queue</h3><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-a-decorator"><a href="#What-is-a-decorator" class="headerlink" title="What is a decorator"></a>What is a decorator</h3><p>A decorator is an expression that evaluates to a function and that takes the target, name, and decorator descriptor as arguments. Also, it optionally returns a decorator descriptor to install on the target object. Let’s define admin decorator for user class at design time,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">admin</span>(<span class="params">isAdmin</span>) </span>&#123;</span><br><span class="line">   <span class="keyword">return</span> <span class="function"><span class="keyword">function</span>(<span class="params">target</span>) </span>&#123;</span><br><span class="line">       target.isAdmin = isAdmin;</span><br><span class="line">   &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">@admin(<span class="literal">true</span>)</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">User</span>() </span>&#123;</span><br><span class="line">&#125;</span><br><span class="line"><span class="built_in">console</span>.log(User.isAdmin); <span class="comment">//true</span></span><br><span class="line"></span><br><span class="line"> @admin(<span class="literal">false</span>)</span><br><span class="line"> <span class="class"><span class="keyword">class</span> <span class="title">User</span>() </span>&#123;</span><br><span class="line"> &#125;</span><br><span class="line"> <span class="built_in">console</span>.log(User.isAdmin); <span class="comment">//false</span></span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-are-the-properties-of-Intl-object"><a href="#What-are-the-properties-of-Intl-object" class="headerlink" title="What are the properties of Intl object"></a>What are the properties of Intl object</h3><p>Below are the list of properties available on Intl object,</p><ol><li><strong>Collator:</strong> These are the objects that enable language-sensitive string comparison.</li><li><strong>DateTimeFormat:</strong> These are the objects that enable language-sensitive date and time formatting.</li><li><strong>ListFormat:</strong> These are the objects that enable language-sensitive list formatting.</li><li><strong>NumberFormat:</strong> Objects that enable language-sensitive number formatting.</li><li><strong>PluralRules:</strong> Objects that enable plural-sensitive formatting and language-specific rules for plurals.</li><li><strong>RelativeTimeFormat:</strong> Objects that enable language-sensitive relative time formatting.</li></ol><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-an-Unary-operator"><a href="#What-is-an-Unary-operator" class="headerlink" title="What is an Unary operator"></a>What is an Unary operator</h3><p>The unary(+) operator is used to convert a variable to a number.If the variable cannot be converted, it will still become a number but with the value NaN. Let’s see this behavior in an action.</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> x = <span class="string">&quot;100&quot;</span>;</span><br><span class="line"><span class="keyword">var</span> y = + x;</span><br><span class="line"><span class="built_in">console</span>.log(<span class="keyword">typeof</span> x, <span class="keyword">typeof</span> y); <span class="comment">// string, number</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> a = <span class="string">&quot;Hello&quot;</span>;</span><br><span class="line"><span class="keyword">var</span> b = + a;</span><br><span class="line"><span class="built_in">console</span>.log(<span class="keyword">typeof</span> a, <span class="keyword">typeof</span> b, b); <span class="comment">// string, number, NaN</span></span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="How-do-you-sort-elements-in-an-array"><a href="#How-do-you-sort-elements-in-an-array" class="headerlink" title="How do you sort elements in an array"></a>How do you sort elements in an array</h3><p>The sort() method is used to sort the elements of an array in place and returns the sorted array. The example usage would be as below,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> months = [<span class="string">&quot;Aug&quot;</span>, <span class="string">&quot;Sep&quot;</span>, <span class="string">&quot;Jan&quot;</span>, <span class="string">&quot;June&quot;</span>];</span><br><span class="line">months.sort();</span><br><span class="line"><span class="built_in">console</span>.log(months); <span class="comment">//  [&quot;Aug&quot;, &quot;Jan&quot;, &quot;June&quot;, &quot;Sep&quot;]</span></span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-the-purpose-of-compareFunction-while-sorting-arrays"><a href="#What-is-the-purpose-of-compareFunction-while-sorting-arrays" class="headerlink" title="What is the purpose of compareFunction while sorting arrays"></a>What is the purpose of compareFunction while sorting arrays</h3><p>The compareFunction is used to define the sort order. If omitted, the array elements are converted to strings, then sorted according to each character’s Unicode code point value. Let’s take an example to see the usage of compareFunction,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> numbers = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">5</span>, <span class="number">3</span>, <span class="number">4</span>];</span><br><span class="line">numbers.sort(<span class="function">(<span class="params">a, b</span>) =&gt;</span> b - a);</span><br><span class="line"><span class="built_in">console</span>.log(numbers); <span class="comment">// [5, 4, 3, 2, 1]</span></span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="How-do-you-reversing-an-array"><a href="#How-do-you-reversing-an-array" class="headerlink" title="How do you reversing an array"></a>How do you reversing an array</h3><p>You can use the reverse() method to reverse the elements in an array. This method is useful to sort an array in descending order. Let’s see the usage of reverse() method in an example,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> numbers = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">5</span>, <span class="number">3</span>, <span class="number">4</span>];</span><br><span class="line">numbers.sort(<span class="function">(<span class="params">a, b</span>) =&gt;</span> b - a);</span><br><span class="line">numbers.reverse();</span><br><span class="line"><span class="built_in">console</span>.log(numbers); <span class="comment">// [1, 2, 3, 4 ,5]</span></span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="How-do-you-find-min-and-max-value-in-an-array"><a href="#How-do-you-find-min-and-max-value-in-an-array" class="headerlink" title="How do you find min and max value in an array"></a>How do you find min and max value in an array</h3><p>You can use <code>Math.min</code> and <code>Math.max</code> methods on array variables to find the minimum and maximum elements within an array. Let’s create two functions to find the min and max value with in an array,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> marks = [<span class="number">50</span>, <span class="number">20</span>, <span class="number">70</span>, <span class="number">60</span>, <span class="number">45</span>, <span class="number">30</span>];</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">findMin</span>(<span class="params">arr</span>) </span>&#123;</span><br><span class="line">  <span class="keyword">return</span> <span class="built_in">Math</span>.min.apply(<span class="literal">null</span>, arr);</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">findMax</span>(<span class="params">arr</span>) </span>&#123;</span><br><span class="line">  <span class="keyword">return</span> <span class="built_in">Math</span>.max.apply(<span class="literal">null</span>, arr);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="built_in">console</span>.log(findMin(marks));</span><br><span class="line"><span class="built_in">console</span>.log(findMax(marks));</span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="How-do-you-find-min-and-max-values-without-Math-functions"><a href="#How-do-you-find-min-and-max-values-without-Math-functions" class="headerlink" title="How do you find min and max values without Math functions"></a>How do you find min and max values without Math functions</h3><p>You can write functions which loop through an array comparing each value with the lowest value or highest value to find the min and max values. Let’s create those functions to find min and max values,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> marks = [<span class="number">50</span>, <span class="number">20</span>, <span class="number">70</span>, <span class="number">60</span>, <span class="number">45</span>, <span class="number">30</span>];</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">findMin</span>(<span class="params">arr</span>) </span>&#123;</span><br><span class="line">  <span class="keyword">var</span> length = arr.length</span><br><span class="line">  <span class="keyword">var</span> min = <span class="literal">Infinity</span>;</span><br><span class="line">  <span class="keyword">while</span> (length--) &#123;</span><br><span class="line">    <span class="keyword">if</span> (arr[length] &lt; min) &#123;</span><br><span class="line">      min = arr[len];</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="keyword">return</span> min;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">findMax</span>(<span class="params">arr</span>) </span>&#123;</span><br><span class="line">  <span class="keyword">var</span> length = arr.length</span><br><span class="line">  <span class="keyword">var</span> max = -<span class="literal">Infinity</span>;</span><br><span class="line">  <span class="keyword">while</span> (len--) &#123;</span><br><span class="line">    <span class="keyword">if</span> (arr[length] &gt; max) &#123;</span><br><span class="line">      max = arr[length];</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="keyword">return</span> max;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="built_in">console</span>.log(findMin(marks));</span><br><span class="line"><span class="built_in">console</span>.log(findMax(marks));</span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-an-empty-statement-and-purpose-of-it"><a href="#What-is-an-empty-statement-and-purpose-of-it" class="headerlink" title="What is an empty statement and purpose of it"></a>What is an empty statement and purpose of it</h3><p>The empty statement is a semicolon (;) indicating that no statement will be executed, even if JavaScript syntax requires one. Since there is no action with an empty statement you might think that it’s usage is quite less, but the empty statement is occasionally useful when you want to create a loop that has an empty body. For example, you can initialize an array with zero values as below,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// Initialize an array a</span></span><br><span class="line"><span class="keyword">for</span>(int i=<span class="number">0</span>; i &lt; a.length; a[i++] = <span class="number">0</span>) ;</span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="How-do-you-get-metadata-of-a-module"><a href="#How-do-you-get-metadata-of-a-module" class="headerlink" title="How do you get metadata of a module"></a>How do you get metadata of a module</h3><p>You can use the <code>import.meta</code> object which is a meta-property exposing context-specific meta data to a JavaScript module. It contains information about the current module, such as the module’s URL. In browsers, you might get different meta data than NodeJS.</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">&lt;script type=<span class="string">&quot;module&quot;</span> src=<span class="string">&quot;welcome-module.js&quot;</span>&gt;&lt;/script&gt;</span><br><span class="line"><span class="built_in">console</span>.log(<span class="keyword">import</span>.meta); <span class="comment">// &#123; url: &quot;file:///home/user/welcome-module.js&quot; &#125;</span></span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-a-comma-operator"><a href="#What-is-a-comma-operator" class="headerlink" title="What is a comma operator"></a>What is a comma operator</h3><p>The comma operator is used to evaluate each of its operands from left to right and returns the value of the last operand. This is totally different from comma usage within arrays, objects, and function arguments and parameters. For example, the usage for numeric expressions would be as below,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> x = <span class="number">1</span>;</span><br><span class="line">x = (x++, x);</span><br><span class="line"></span><br><span class="line"><span class="built_in">console</span>.log(x); <span class="comment">// 2</span></span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-the-advantage-of-a-comma-operator"><a href="#What-is-the-advantage-of-a-comma-operator" class="headerlink" title="What is the advantage of a comma operator"></a>What is the advantage of a comma operator</h3><p>It is normally used to include multiple expressions in a location that requires a single expression. One of the common usages of this comma operator is to supply multiple parameters in a <code>for</code> loop. For example, the below for loop uses multiple expressions in a single location using comma operator,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">for</span> (<span class="keyword">var</span> a = <span class="number">0</span>, b =<span class="number">10</span>; a &lt;= <span class="number">10</span>; a++, b--)</span><br></pre></td></tr></table></figure><p>You can also use the comma operator in a return statement where it processes before returning.</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">myFunction</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">   <span class="keyword">var</span> a = <span class="number">1</span>;</span><br><span class="line">   <span class="keyword">return</span> (a += <span class="number">10</span>, a); <span class="comment">// 11</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-typescript"><a href="#What-is-typescript" class="headerlink" title="What is typescript"></a>What is typescript</h3><p>TypeScript is a typed superset of JavaScript created by Microsoft that adds optional types, classes, async/await, and many other features, and compiles to plain JavaScript. Angular built entirely in TypeScript and used as a primary language. You can install it globally as</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npm install -g typescript</span><br></pre></td></tr></table></figure><p>Let’s see a simple example of TypeScript usage,</p><figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">greeting</span>(<span class="params">name: <span class="built_in">string</span></span>): <span class="title">string</span> </span>&#123;</span><br><span class="line">   <span class="keyword">return</span> <span class="string">&quot;Hello, &quot;</span> + name;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> user = <span class="string">&quot;Sudheer&quot;</span>;</span><br><span class="line"></span><br><span class="line"><span class="built_in">console</span>.log(greeting(user));</span><br></pre></td></tr></table></figure><p>The greeting method allows only string type as argument.</p><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-are-the-differences-between-javascript-and-typescript"><a href="#What-are-the-differences-between-javascript-and-typescript" class="headerlink" title="What are the differences between javascript and typescript"></a>What are the differences between javascript and typescript</h3><p>Below are the list of differences between javascript and typescript,</p><table><thead><tr><th>feature</th><th>typescript</th><th>javascript</th></tr></thead><tbody><tr><td>Language paradigm</td><td>Object oriented programming language</td><td>Scripting language</td></tr><tr><td>Typing support</td><td>Supports static typing</td><td>It has dynamic typing</td></tr><tr><td>Modules</td><td>Supported</td><td>Not supported</td></tr><tr><td>Interface</td><td>It has interfaces concept</td><td>Doesn’t support interfaces</td></tr><tr><td>Optional parameters</td><td>Functions support optional parameters</td><td>No support of optional parameters for functions</td></tr></tbody></table><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-are-the-advantages-of-typescript-over-javascript"><a href="#What-are-the-advantages-of-typescript-over-javascript" class="headerlink" title="What are the advantages of typescript over javascript"></a>What are the advantages of typescript over javascript</h3><p>Below are some of the advantages of typescript over javascript,</p><ol><li>TypeScript is able to find compile time errors at the development time only and it makes sures less runtime errors. Whereas javascript is an interpreted language.</li><li>TypeScript is strongly-typed or supports static typing which allows for checking type correctness at compile time. This is not available in javascript.</li><li>TypeScript compiler can compile the .ts files into ES3,ES4 and ES5 unlike ES6 features of javascript which may not be supported in some browsers.</li></ol><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-an-object-initializer"><a href="#What-is-an-object-initializer" class="headerlink" title="What is an object initializer"></a>What is an object initializer</h3><p>An object initializer is an expression that describes the initialization of an Object. The syntax for this expression is represented as a comma-delimited list of zero or more pairs of property names and associated values of an object, enclosed in curly braces ({}). This is also known as literal notation. It is one of the ways to create an object.</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> initObject = &#123;<span class="attr">a</span>: <span class="string">&#x27;John&#x27;</span>, <span class="attr">b</span>: <span class="number">50</span>, <span class="attr">c</span>: &#123;&#125;&#125;;</span><br><span class="line"></span><br><span class="line"><span class="built_in">console</span>.log(initObject.a); <span class="comment">// John</span></span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-a-constructor-method"><a href="#What-is-a-constructor-method" class="headerlink" title="What is a constructor method"></a>What is a constructor method</h3><p>The constructor method is a special method for creating and initializing an object created within a class. If you do not specify a constructor method, a default constructor is used. The example usage of constructor would be as below,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Employee</span> </span>&#123;</span><br><span class="line">  <span class="function"><span class="title">constructor</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">    <span class="built_in">this</span>.name = <span class="string">&quot;John&quot;</span>;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> employeeObject = <span class="keyword">new</span> Employee();</span><br><span class="line"></span><br><span class="line"><span class="built_in">console</span>.log(employeeObject.name); <span class="comment">// John</span></span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-happens-if-you-write-constructor-more-than-once-in-a-class"><a href="#What-happens-if-you-write-constructor-more-than-once-in-a-class" class="headerlink" title="What happens if you write constructor more than once in a class"></a>What happens if you write constructor more than once in a class</h3><p>The “constructor” in a class is a special method and it should be defined only once in a class. i.e, If you write a constructor method more than once in a class it will throw a <code>SyntaxError</code> error.</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Employee</span> </span>&#123;</span><br><span class="line">  <span class="function"><span class="title">constructor</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">    <span class="built_in">this</span>.name = <span class="string">&quot;John&quot;</span>;</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="function"><span class="title">constructor</span>(<span class="params"></span>)</span> &#123;   <span class="comment">//  Uncaught SyntaxError: A class may only have one constructor</span></span><br><span class="line">    <span class="built_in">this</span>.age = <span class="number">30</span>;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> employeeObject = <span class="keyword">new</span> Employee();</span><br><span class="line"></span><br><span class="line"><span class="built_in">console</span>.log(employeeObject.name);</span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="How-do-you-call-the-constructor-of-a-parent-class"><a href="#How-do-you-call-the-constructor-of-a-parent-class" class="headerlink" title="How do you call the constructor of a parent class"></a>How do you call the constructor of a parent class</h3><p>You can use the <code>super</code> keyword to call the constructor of a parent class. Remember that <code>super()</code> must be called before using ‘this’ reference. Otherwise it will cause a reference error. Let’s the usage of it,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Square</span> <span class="keyword">extends</span> <span class="title">Rectangle</span> </span>&#123;</span><br><span class="line">  <span class="function"><span class="title">constructor</span>(<span class="params">length</span>)</span> &#123;</span><br><span class="line">    <span class="built_in">super</span>(length, length);</span><br><span class="line">    <span class="built_in">this</span>.name = <span class="string">&#x27;Square&#x27;</span>;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">get</span> <span class="title">area</span>() &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="built_in">this</span>.width * <span class="built_in">this</span>.height;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">set</span> <span class="title">area</span>(<span class="params">value</span>) &#123;</span><br><span class="line">    <span class="built_in">this</span>.area = value;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="How-do-you-get-the-prototype-of-an-object"><a href="#How-do-you-get-the-prototype-of-an-object" class="headerlink" title="How do you get the prototype of an object"></a>How do you get the prototype of an object</h3><p>You can use the <code>Object.getPrototypeOf(obj)</code> method to return the prototype of the specified object. i.e. The value of the internal <code>prototype</code> property. If there are no inherited properties then <code>null</code> value is returned.</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> newPrototype = &#123;&#125;;</span><br><span class="line"><span class="keyword">const</span> newObject = <span class="built_in">Object</span>.create(newPrototype);</span><br><span class="line"></span><br><span class="line"><span class="built_in">console</span>.log(<span class="built_in">Object</span>.getPrototypeOf(newObject) === newPrototype); <span class="comment">// true</span></span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-happens-If-I-pass-string-type-for-getPrototype-method"><a href="#What-happens-If-I-pass-string-type-for-getPrototype-method" class="headerlink" title="What happens If I pass string type for getPrototype method"></a>What happens If I pass string type for getPrototype method</h3><p>In ES5, it will throw a TypeError exception if the obj parameter isn’t an object. Whereas in ES2015, the parameter will be coerced to an <code>Object</code>.</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// ES5</span></span><br><span class="line"><span class="built_in">Object</span>.getPrototypeOf(<span class="string">&#x27;James&#x27;</span>); <span class="comment">// TypeError: &quot;James&quot; is not an object</span></span><br><span class="line"><span class="comment">// ES2015</span></span><br><span class="line"><span class="built_in">Object</span>.getPrototypeOf(<span class="string">&#x27;James&#x27;</span>); <span class="comment">// String.prototype</span></span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="How-do-you-set-prototype-of-one-object-to-another"><a href="#How-do-you-set-prototype-of-one-object-to-another" class="headerlink" title="How do you set prototype of one object to another"></a>How do you set prototype of one object to another</h3><p>You can use the <code>Object.setPrototypeOf()</code> method that sets the prototype (i.e., the internal <code>Prototype</code> property) of a specified object to another object or null. For example, if you want to set prototype of a square object to rectangle object would be as follows,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">Object</span>.setPrototypeOf(Square.prototype, Rectangle.prototype);</span><br><span class="line"><span class="built_in">Object</span>.setPrototypeOf(&#123;&#125;, <span class="literal">null</span>);</span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="How-do-you-check-whether-an-object-can-be-extendable-or-not"><a href="#How-do-you-check-whether-an-object-can-be-extendable-or-not" class="headerlink" title="How do you check whether an object can be extendable or not"></a>How do you check whether an object can be extendable or not</h3><p>The <code>Object.isExtensible()</code> method is used to determine if an object is extendable or not. i.e, Whether it can have new properties added to it or not.</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> newObject = &#123;&#125;;</span><br><span class="line"><span class="built_in">console</span>.log(<span class="built_in">Object</span>.isExtensible(newObject)); <span class="comment">//true</span></span><br></pre></td></tr></table></figure><p><strong>Note:</strong> By default, all the objects are extendable. i.e, The new properties can be added or modified.</p><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="How-do-you-prevent-an-object-to-extend"><a href="#How-do-you-prevent-an-object-to-extend" class="headerlink" title="How do you prevent an object to extend"></a>How do you prevent an object to extend</h3><p>The <code>Object.preventExtensions()</code> method is used to prevent new properties from ever being added to an object. In other words, it prevents future extensions to the object. Let’s see the usage of this property,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> newObject = &#123;&#125;;</span><br><span class="line"><span class="built_in">Object</span>.preventExtensions(newObject); <span class="comment">// NOT extendable</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">try</span> &#123;</span><br><span class="line">  <span class="built_in">Object</span>.defineProperty(newObject, <span class="string">&#x27;newProperty&#x27;</span>, &#123; <span class="comment">// Adding new property</span></span><br><span class="line">    value: <span class="number">100</span></span><br><span class="line">  &#125;);</span><br><span class="line">&#125; <span class="keyword">catch</span> (e) &#123;</span><br><span class="line">  <span class="built_in">console</span>.log(e); <span class="comment">// TypeError: Cannot define property newProperty, object is not extensible</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-are-the-different-ways-to-make-an-object-non-extensible"><a href="#What-are-the-different-ways-to-make-an-object-non-extensible" class="headerlink" title="What are the different ways to make an object non-extensible"></a>What are the different ways to make an object non-extensible</h3><p>You can mark an object non-extensible in 3 ways,</p><ol><li>Object.preventExtensions</li><li>Object.seal</li><li>Object.freeze</li></ol><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> newObject = &#123;&#125;;</span><br><span class="line"></span><br><span class="line"><span class="built_in">Object</span>.preventExtensions(newObject); <span class="comment">// Prevent objects are non-extensible</span></span><br><span class="line"><span class="built_in">Object</span>.isExtensible(newObject); <span class="comment">// false</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> sealedObject = <span class="built_in">Object</span>.seal(&#123;&#125;); <span class="comment">// Sealed objects are non-extensible</span></span><br><span class="line"><span class="built_in">Object</span>.isExtensible(sealedObject); <span class="comment">// false</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> frozenObject = <span class="built_in">Object</span>.freeze(&#123;&#125;); <span class="comment">// Frozen objects are non-extensible</span></span><br><span class="line"><span class="built_in">Object</span>.isExtensible(frozenObject); <span class="comment">// false</span></span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="How-do-you-define-multiple-properties-on-an-object"><a href="#How-do-you-define-multiple-properties-on-an-object" class="headerlink" title="How do you define multiple properties on an object"></a>How do you define multiple properties on an object</h3><p>The <code>Object.defineProperties()</code> method is used to define new or modify existing properties directly on an object and returning the object. Let’s define multiple properties on an empty object,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> newObject = &#123;&#125;;</span><br><span class="line"></span><br><span class="line"><span class="built_in">Object</span>.defineProperties(newObject, &#123;</span><br><span class="line">  newProperty1: &#123;</span><br><span class="line">    value: <span class="string">&#x27;John&#x27;</span>,</span><br><span class="line">    writable: <span class="literal">true</span></span><br><span class="line">  &#125;,</span><br><span class="line">  newProperty2: &#123;&#125;</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-MEAN-in-javascript"><a href="#What-is-MEAN-in-javascript" class="headerlink" title="What is MEAN in javascript"></a>What is MEAN in javascript</h3><p>The MEAN (MongoDB, Express, AngularJS, and Node.js) stack is the most popular open-source JavaScript software tech stack available for building dynamic web apps where you can write both the server-side and client-side halves of the web project entirely in JavaScript.</p><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-Is-Obfuscation-in-javascript"><a href="#What-Is-Obfuscation-in-javascript" class="headerlink" title="What Is Obfuscation in javascript"></a>What Is Obfuscation in javascript</h3><p>Obfuscation is the deliberate act of creating obfuscated javascript code(i.e, source or machine code) that is difficult for humans to understand. It is something similar to encryption, but a machine can understand the code and execute it.<br>Let’s see the below function before Obfuscation,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">greeting</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">   <span class="built_in">console</span>.log(<span class="string">&#x27;Hello, welcome to JS world&#x27;</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>And after the code Obfuscation, it would be appeared as below,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">eval</span>(<span class="function"><span class="keyword">function</span>(<span class="params">p,a,c,k,e,d</span>)</span>&#123;e=<span class="function"><span class="keyword">function</span>(<span class="params">c</span>)</span>&#123;<span class="keyword">return</span> c&#125;;<span class="keyword">if</span>(!<span class="string">&#x27;&#x27;</span>.replace(<span class="regexp">/^/</span>,<span class="built_in">String</span>))&#123;<span class="keyword">while</span>(c--)&#123;d[c]=k[c]||c&#125;k=[<span class="function"><span class="keyword">function</span>(<span class="params">e</span>)</span>&#123;<span class="keyword">return</span> d[e]&#125;];e=<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;<span class="keyword">return</span><span class="string">&#x27;\\w+&#x27;</span>&#125;;c=<span class="number">1</span>&#125;;<span class="keyword">while</span>(c--)&#123;<span class="keyword">if</span>(k[c])&#123;p=p.replace(<span class="keyword">new</span> <span class="built_in">RegExp</span>(<span class="string">&#x27;\\b&#x27;</span>+e(c)+<span class="string">&#x27;\\b&#x27;</span>,<span class="string">&#x27;g&#x27;</span>),k[c])&#125;&#125;<span class="keyword">return</span> p&#125;(<span class="string">&#x27;2 1()&#123;0.3(\&#x27;4, 7 6 5 8\&#x27;)&#125;&#x27;</span>,<span class="number">9</span>,<span class="number">9</span>,<span class="string">&#x27;console|greeting|function|log|Hello|JS|to|welcome|world&#x27;</span>.split(<span class="string">&#x27;|&#x27;</span>),<span class="number">0</span>,&#123;&#125;))</span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="Why-do-you-need-Obfuscation"><a href="#Why-do-you-need-Obfuscation" class="headerlink" title="Why do you need Obfuscation"></a>Why do you need Obfuscation</h3><p>Below are the few reasons for Obfuscation,</p><ol><li>The Code size will be reduced. So data transfers between server and client will be fast.</li><li>It hides the business logic from outside world and protects the code from others</li><li>Reverse engineering is highly difficult</li><li>The download time will be reduced</li></ol><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-Minification"><a href="#What-is-Minification" class="headerlink" title="What is Minification"></a>What is Minification</h3><p>Minification is the process of removing all unnecessary characters(empty spaces are removed) and variables will be renamed without changing it’s functionality. It is also a type of obfuscation .</p><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-are-the-advantages-of-minification"><a href="#What-are-the-advantages-of-minification" class="headerlink" title="What are the advantages of minification"></a>What are the advantages of minification</h3><p>Normally it is recommended to use minification for heavy traffic and intensive requirements of resources. It reduces file sizes with below benefits,</p><ol><li>Decreases loading times of a web page</li><li>Saves bandwidth usages</li></ol><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-are-the-differences-between-Obfuscation-and-Encryption"><a href="#What-are-the-differences-between-Obfuscation-and-Encryption" class="headerlink" title="What are the differences between Obfuscation and Encryption"></a>What are the differences between Obfuscation and Encryption</h3><p>Below are the main differences between Obfuscation and Encryption,</p><table><thead><tr><th>Feature</th><th>Obfuscation</th><th>Encryption</th></tr></thead><tbody><tr><td>Definition</td><td>Changing the form of any data in any other form</td><td>Changing the form of information to an unreadable format by using a key</td></tr><tr><td>A key to decode</td><td>It can be decoded without any key</td><td>It is required</td></tr><tr><td>Target data format</td><td>It will be converted to a complex form</td><td>Converted into an unreadable format</td></tr></tbody></table><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-are-the-common-tools-used-for-minification"><a href="#What-are-the-common-tools-used-for-minification" class="headerlink" title="What are the common tools used for minification"></a>What are the common tools used for minification</h3><p>There are many online/offline tools to minify the javascript files,</p><ol><li>Google’s Closure Compiler</li><li>UglifyJS2</li><li>jsmin</li><li>javascript-minifier.com/</li><li>prettydiff.com</li></ol><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="How-do-you-perform-form-validation-using-javascript"><a href="#How-do-you-perform-form-validation-using-javascript" class="headerlink" title="How do you perform form validation using javascript"></a>How do you perform form validation using javascript</h3><p>JavaScript can be used to perform HTML form validation. For example, if the form field is empty, the function needs to notify, and return false, to prevent the form being submitted.<br>Lets’ perform user login in an html form,</p><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">form</span> <span class="attr">name</span>=<span class="string">&quot;myForm&quot;</span> <span class="attr">onsubmit</span>=<span class="string">&quot;return validateForm()&quot;</span> <span class="attr">method</span>=<span class="string">&quot;post&quot;</span>&gt;</span></span><br><span class="line">User name: <span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">&quot;text&quot;</span> <span class="attr">name</span>=<span class="string">&quot;uname&quot;</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">&quot;submit&quot;</span> <span class="attr">value</span>=<span class="string">&quot;Submit&quot;</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">form</span>&gt;</span></span><br></pre></td></tr></table></figure><p>And the validation on user login is below,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">validateForm</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">  <span class="keyword">var</span> x = <span class="built_in">document</span>.forms[<span class="string">&quot;myForm&quot;</span>][<span class="string">&quot;uname&quot;</span>].value;</span><br><span class="line">  <span class="keyword">if</span> (x == <span class="string">&quot;&quot;</span>) &#123;</span><br><span class="line">    alert(<span class="string">&quot;The username shouldn&#x27;t be empty&quot;</span>);</span><br><span class="line">    <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="How-do-you-perform-form-validation-without-javascript"><a href="#How-do-you-perform-form-validation-without-javascript" class="headerlink" title="How do you perform form validation without javascript"></a>How do you perform form validation without javascript</h3><p>You can perform HTML form validation automatically without using javascript. The validation enabled by applying the <code>required</code> attribute to prevent form submission when the input is empty.</p><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">form</span> <span class="attr">method</span>=<span class="string">&quot;post&quot;</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">&quot;text&quot;</span> <span class="attr">name</span>=<span class="string">&quot;uname&quot;</span> <span class="attr">required</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">&quot;submit&quot;</span> <span class="attr">value</span>=<span class="string">&quot;Submit&quot;</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">form</span>&gt;</span></span><br></pre></td></tr></table></figure><p><strong>Note:</strong> Automatic form validation does not work in Internet Explorer 9 or earlier.</p><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-are-the-DOM-methods-available-for-constraint-validation"><a href="#What-are-the-DOM-methods-available-for-constraint-validation" class="headerlink" title="What are the DOM methods available for constraint validation"></a>What are the DOM methods available for constraint validation</h3><p>The below DOM methods are available for constraint validation on an invalid input,</p><ol><li>checkValidity(): It returns true if an input element contains valid data.</li><li>setCustomValidity(): It is used to set the validationMessage property of an input element.<br>Let’s take an user login form with DOM validations</li></ol><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">myFunction</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">  <span class="keyword">var</span> userName = <span class="built_in">document</span>.getElementById(<span class="string">&quot;uname&quot;</span>);</span><br><span class="line">  <span class="keyword">if</span> (!userName.checkValidity()) &#123;</span><br><span class="line">    <span class="built_in">document</span>.getElementById(<span class="string">&quot;message&quot;</span>).innerHTML = userName.validationMessage;</span><br><span class="line">  &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">    <span class="built_in">document</span>.getElementById(<span class="string">&quot;message&quot;</span>).innerHTML = <span class="string">&quot;Entered a valid username&quot;</span>;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-are-the-available-constraint-validation-DOM-properties"><a href="#What-are-the-available-constraint-validation-DOM-properties" class="headerlink" title="What are the available constraint validation DOM properties"></a>What are the available constraint validation DOM properties</h3><p>Below are the list of some of the constraint validation DOM properties available,</p><ol><li>validity: It provides a list of boolean properties related to the validity of an input element.</li><li>validationMessage: It displays the message when the validity is false.</li><li>willValidate: It indicates if an input element will be validated or not.</li></ol><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-are-the-list-of-validity-properties"><a href="#What-are-the-list-of-validity-properties" class="headerlink" title="What are the list of validity properties"></a>What are the list of validity properties</h3><p>The validity property of an input element provides a set of properties related to the validity of data.</p><ol><li>customError: It returns true, if a custom validity message is set.</li><li>patternMismatch: It returns true, if an element’s value does not match its pattern attribute.</li><li>rangeOverflow: It returns true, if an element’s value is greater than its max attribute.</li><li>rangeUnderflow: It returns true, if an element’s value is less than its min attribute.</li><li>stepMismatch: It returns true, if an element’s value is invalid according to step attribute.</li><li>tooLong: It returns true, if an element’s value exceeds its maxLength attribute.</li><li>typeMismatch: It returns true, if an element’s value is invalid according to type attribute.</li><li>valueMissing: It returns true, if an element with a required attribute has no value.</li><li>valid: It returns true, if an element’s value is valid.</li></ol><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="Give-an-example-usage-of-rangeOverflow-property"><a href="#Give-an-example-usage-of-rangeOverflow-property" class="headerlink" title="Give an example usage of rangeOverflow property"></a>Give an example usage of rangeOverflow property</h3><p>If an element’s value is greater than its max attribute then rangeOverflow property returns true. For example, the below form submission throws an error if the value is more than 100,</p><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">input</span> <span class="attr">id</span>=<span class="string">&quot;age&quot;</span> <span class="attr">type</span>=<span class="string">&quot;number&quot;</span> <span class="attr">max</span>=<span class="string">&quot;100&quot;</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">button</span> <span class="attr">onclick</span>=<span class="string">&quot;myOverflowFunction()&quot;</span>&gt;</span>OK<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br></pre></td></tr></table></figure><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">myOverflowFunction</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">  <span class="keyword">if</span> (<span class="built_in">document</span>.getElementById(<span class="string">&quot;age&quot;</span>).validity.rangeOverflow) &#123;</span><br><span class="line">    alert(<span class="string">&quot;The mentioned age is not allowed&quot;</span>);</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="Is-enums-feature-available-in-javascript"><a href="#Is-enums-feature-available-in-javascript" class="headerlink" title="Is enums feature available in javascript"></a>Is enums feature available in javascript</h3><p>No, javascript does not natively support enums. But there are different kinds of solutions to simulate them even though they may not provide exact equivalents. For example, you can use freeze or seal on object,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> DaysEnum = <span class="built_in">Object</span>.freeze(&#123;<span class="string">&quot;monday&quot;</span>:<span class="number">1</span>, <span class="string">&quot;tuesday&quot;</span>:<span class="number">2</span>, <span class="string">&quot;wednesday&quot;</span>:<span class="number">3</span>, ...&#125;)</span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-an-enum"><a href="#What-is-an-enum" class="headerlink" title="What is an enum"></a>What is an enum</h3><p>An enum is a type restricting variables to one value from a predefined set of constants. JavaScript has no enums but typescript provides built-in enum support.</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">enum Color &#123;</span><br><span class="line"> RED, GREEN, BLUE</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="How-do-you-list-all-properties-of-an-object"><a href="#How-do-you-list-all-properties-of-an-object" class="headerlink" title="How do you list all properties of an object"></a>How do you list all properties of an object</h3><p>You can use the <code>Object.getOwnPropertyNames()</code> method which returns an array of all properties found directly in a given object. Let’s the usage of it in an example,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> newObject = &#123;</span><br><span class="line">  a: <span class="number">1</span>,</span><br><span class="line">  b: <span class="number">2</span>,</span><br><span class="line">  c: <span class="number">3</span></span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="built_in">console</span>.log(<span class="built_in">Object</span>.getOwnPropertyNames(newObject));  [<span class="string">&quot;a&quot;</span>, <span class="string">&quot;b&quot;</span>, <span class="string">&quot;c&quot;</span>]</span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="How-do-you-get-property-descriptors-of-an-object"><a href="#How-do-you-get-property-descriptors-of-an-object" class="headerlink" title="How do you get property descriptors of an object"></a>How do you get property descriptors of an object</h3><p>You can use the <code>Object.getOwnPropertyDescriptors()</code> method which returns all own property descriptors of a given object. The example usage of this method is below,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"> <span class="keyword">const</span> newObject = &#123;</span><br><span class="line">   a: <span class="number">1</span>,</span><br><span class="line">   b: <span class="number">2</span>,</span><br><span class="line">   c: <span class="number">3</span></span><br><span class="line"> &#125;;</span><br><span class="line"><span class="keyword">const</span> descriptorsObject = <span class="built_in">Object</span>.getOwnPropertyDescriptors(newObject);</span><br><span class="line"><span class="built_in">console</span>.log(descriptorsObject.a.writable); <span class="comment">//true</span></span><br><span class="line"><span class="built_in">console</span>.log(descriptorsObject.a.configurable); <span class="comment">//true</span></span><br><span class="line"><span class="built_in">console</span>.log(descriptorsObject.a.enumerable); <span class="comment">//true</span></span><br><span class="line"><span class="built_in">console</span>.log(descriptorsObject.a.value); <span class="comment">// 1</span></span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-are-the-attributes-provided-by-a-property-descriptor"><a href="#What-are-the-attributes-provided-by-a-property-descriptor" class="headerlink" title="What are the attributes provided by a property descriptor"></a>What are the attributes provided by a property descriptor</h3><p>A property descriptor is a record which has the following attributes</p><ol><li>value: The value associated with the property</li><li>writable: Determines whether the value associated with the property can be changed or not</li><li>configurable: Returns true if the type of this property descriptor can be changed and if the property can be deleted from the corresponding object.</li><li>enumerable: Determines whether the property appears during enumeration of the properties on the corresponding object or not.</li><li>set: A function which serves as a setter for the property</li><li>get: A function which serves as a getter for the property</li></ol><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="How-do-you-extend-classes"><a href="#How-do-you-extend-classes" class="headerlink" title="How do you extend classes"></a>How do you extend classes</h3><p>The <code>extends</code> keyword is used in class declarations/expressions to create a class which is a child of another class. It can be used to subclass custom classes as well as built-in objects. The syntax would be as below,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">ChildClass</span> <span class="keyword">extends</span> <span class="title">ParentClass</span> </span>&#123; ... &#125;</span><br></pre></td></tr></table></figure><p>Let’s take an example of Square subclass from Polygon parent class,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Square</span> <span class="keyword">extends</span> <span class="title">Rectangle</span> </span>&#123;</span><br><span class="line">  <span class="function"><span class="title">constructor</span>(<span class="params">length</span>)</span> &#123;</span><br><span class="line">    <span class="built_in">super</span>(length, length);</span><br><span class="line">    <span class="built_in">this</span>.name = <span class="string">&#x27;Square&#x27;</span>;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">get</span> <span class="title">area</span>() &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="built_in">this</span>.width * <span class="built_in">this</span>.height;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">set</span> <span class="title">area</span>(<span class="params">value</span>) &#123;</span><br><span class="line">    <span class="built_in">this</span>.area = value;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="How-do-I-modify-the-url-without-reloading-the-page"><a href="#How-do-I-modify-the-url-without-reloading-the-page" class="headerlink" title="How do I modify the url without reloading the page"></a>How do I modify the url without reloading the page</h3><p>The <code>window.location.url</code> property will be helpful to modify the url but it reloads the page. HTML5 introduced the <code>history.pushState()</code> and <code>history.replaceState()</code> methods, which allow you to add and modify history entries, respectively. For example, you can use pushState as below,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">window</span>.history.pushState(<span class="string">&#x27;page2&#x27;</span>, <span class="string">&#x27;Title&#x27;</span>, <span class="string">&#x27;/page2.html&#x27;</span>);</span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="How-do-you-check-whether-an-array-includes-a-particular-value-or-not"><a href="#How-do-you-check-whether-an-array-includes-a-particular-value-or-not" class="headerlink" title="How do you check whether an array includes a particular value or not"></a>How do you check whether an array includes a particular value or not</h3><p>The <code>Array#includes()</code> method is used to determine whether an array includes a particular value among its entries by returning either true or false. Let’s see an example to find an element(numeric and string) within an array.</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> numericArray = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>];</span><br><span class="line"><span class="built_in">console</span>.log(numericArray.includes(<span class="number">3</span>)); <span class="comment">// true</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> stringArray = [<span class="string">&#x27;green&#x27;</span>, <span class="string">&#x27;yellow&#x27;</span>, <span class="string">&#x27;blue&#x27;</span>];</span><br><span class="line"><span class="built_in">console</span>.log(stringArray.includes(<span class="string">&#x27;blue&#x27;</span>)); <span class="comment">//true</span></span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="How-do-you-compare-scalar-arrays"><a href="#How-do-you-compare-scalar-arrays" class="headerlink" title="How do you compare scalar arrays"></a>How do you compare scalar arrays</h3><p>You can use length and every method of arrays to compare two scalar(compared directly using ===) arrays. The combination of these expressions can give the expected result,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line">     <span class="keyword">const</span> arrayFirst = [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>];</span><br><span class="line">     <span class="keyword">const</span> arraySecond = [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>];</span><br><span class="line">     <span class="built_in">console</span>.log(arrayFirst.length === arraySecond.length &amp;&amp; arrayFirst.every(<span class="function">(<span class="params">value, index</span>) =&gt;</span> value === arraySecond[index])); <span class="comment">// true</span></span><br><span class="line">     <span class="string">``</span><span class="string">``</span></span><br><span class="line"></span><br><span class="line">     If you would like to compare arrays irrespective <span class="keyword">of</span> order then you should sort them before,</span><br><span class="line"></span><br><span class="line">     <span class="string">``</span><span class="string">`javascript</span></span><br><span class="line"><span class="string">     const arrayFirst = [2,3,1,4,5];</span></span><br><span class="line"><span class="string">     const arraySecond = [1,2,3,4,5];</span></span><br><span class="line"><span class="string">     console.log(arrayFirst.length === arraySecond.length &amp;&amp; arrayFirst.sort().every((value, index) =&gt; value === arraySecond[index])); //true</span></span><br><span class="line"><span class="string">     `</span><span class="string">``</span><span class="string">`</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">     **[⬆ Back to Top](#table-of-contents)**</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">287. ### How to get the value from get parameters</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">     The `</span><span class="keyword">new</span> URL()<span class="string">` object accepts the url string and `</span>searchParams<span class="string">` property of this object can be used to access the get parameters. Remember that you may need to use polyfill or `</span><span class="built_in">window</span>.location<span class="string">` to access the URL in older browsers(including IE).</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">     `</span><span class="string">``</span>javascript</span><br><span class="line">     <span class="keyword">let</span> urlString = <span class="string">&quot;http://www.some-domain.com/about.html?x=1&amp;y=2&amp;z=3&quot;</span>; <span class="comment">//window.location.href</span></span><br><span class="line">     <span class="keyword">let</span> url = <span class="keyword">new</span> URL(urlString);</span><br><span class="line">     <span class="keyword">let</span> parameterZ = url.searchParams.get(<span class="string">&quot;z&quot;</span>);</span><br><span class="line">     <span class="built_in">console</span>.log(parameterZ); <span class="comment">// 3</span></span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="How-do-you-print-numbers-with-commas-as-thousand-separators"><a href="#How-do-you-print-numbers-with-commas-as-thousand-separators" class="headerlink" title="How do you print numbers with commas as thousand separators"></a>How do you print numbers with commas as thousand separators</h3><p>You can use the <code>Number.prototype.toLocaleString()</code> method which returns a string with a language-sensitive representation such as thousand separator,currency etc of this number.</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">convertToThousandFormat</span>(<span class="params">x</span>)</span>&#123;</span><br><span class="line">  <span class="keyword">return</span> x.toLocaleString(); <span class="comment">// 12,345.679</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="built_in">console</span>.log(convertToThousandFormat(<span class="number">12345.6789</span>));</span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-the-difference-between-java-and-javascript"><a href="#What-is-the-difference-between-java-and-javascript" class="headerlink" title="What is the difference between java and javascript"></a>What is the difference between java and javascript</h3><p>Both are totally unrelated programming languages and no relation between them. Java is statically typed, compiled, runs on its own VM. Whereas Javascript is dynamically typed, interpreted, and runs in a browser and nodejs environments. Let’s see the major differences in a tabular format,</p><table><thead><tr><th>Feature</th><th>Java</th><th>JavaScript</th></tr></thead><tbody><tr><td>Typed</td><td>It’s a strongly typed language</td><td>It’s a dynamic typed language</td></tr><tr><td>Paradigm</td><td>Object oriented programming</td><td>Prototype based programming</td></tr><tr><td>Scoping</td><td>Block scoped</td><td>Function-scoped</td></tr><tr><td>Concurrency</td><td>Thread based</td><td>event based</td></tr><tr><td>Memory</td><td>Uses more memory</td><td>Uses less memory. Hence it will be used for web pages</td></tr></tbody></table><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="Is-javascript-supports-namespace"><a href="#Is-javascript-supports-namespace" class="headerlink" title="Is javascript supports namespace"></a>Is javascript supports namespace</h3><p>JavaScript doesn’t support namespace by default. So if you create any element(function, method, object, variable) then it becomes global and pollutes the global namespace. Let’s take an example of defining two functions without any namespace,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">func1</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">&quot;This is a first definition&quot;</span>);</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">func1</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">&quot;This is a second definition&quot;</span>);</span><br><span class="line">&#125;</span><br><span class="line">func1(); <span class="comment">// This is a second definition</span></span><br></pre></td></tr></table></figure><p>It always calls the second function definition. In this case, namespace will solve the name collision problem.</p><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="How-do-you-declare-namespace"><a href="#How-do-you-declare-namespace" class="headerlink" title="How do you declare namespace"></a>How do you declare namespace</h3><p>Even though JavaScript lacks namespaces, we can use Objects , IIFE to create namespaces.</p><ol><li><strong>Using Object Literal Notation:</strong> Let’s wrap variables and functions inside an Object literal which acts as a namespace. After that you can access them using object notation</li></ol><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> namespaceOne = &#123;</span><br><span class="line">   <span class="function"><span class="keyword">function</span> <span class="title">func1</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">       <span class="built_in">console</span>.log(<span class="string">&quot;This is a first definition&quot;</span>);</span><br><span class="line">   &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">var</span> namespaceTwo = &#123;</span><br><span class="line">     <span class="function"><span class="keyword">function</span> <span class="title">func1</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">         <span class="built_in">console</span>.log(<span class="string">&quot;This is a second definition&quot;</span>);</span><br><span class="line">     &#125;</span><br><span class="line"> &#125;</span><br><span class="line">namespaceOne.func1(); <span class="comment">// This is a first definition</span></span><br><span class="line">namespaceTwo.func1(); <span class="comment">// This is a second definition</span></span><br></pre></td></tr></table></figure><ol><li><strong>Using IIFE (Immediately invoked function expression):</strong> The outer pair of parentheses of IIFE creates a local scope for all the code inside of it and makes the anonymous function a function expression. Due to that, you can create the same function in two different function expressions to act as a namespace.</li></ol><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">(<span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line"> <span class="function"><span class="keyword">function</span> <span class="title">fun1</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">   <span class="built_in">console</span>.log(<span class="string">&quot;This is a first definition&quot;</span>);</span><br><span class="line">   &#125; fun1();</span><br><span class="line">&#125;());</span><br><span class="line"></span><br><span class="line">(<span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">fun1</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">       <span class="built_in">console</span>.log(<span class="string">&quot;This is a second definition&quot;</span>);</span><br><span class="line">   &#125; fun1();</span><br><span class="line"> &#125;());</span><br></pre></td></tr></table></figure><ol><li><strong>Using a block and a let/const declaration:</strong> In ECMAScript 6, you can simply use a block and a let declaration to restrict the scope of a variable to a block.</li></ol><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line"> <span class="keyword">let</span> myFunction= <span class="function"><span class="keyword">function</span> <span class="title">fun1</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line"> <span class="built_in">console</span>.log(<span class="string">&quot;This is a first definition&quot;</span>);</span><br><span class="line"> &#125;</span><br><span class="line"> myFunction();</span><br><span class="line">&#125;</span><br><span class="line"> <span class="comment">//myFunction(): ReferenceError: myFunction is not defined.</span></span><br><span class="line"></span><br><span class="line">&#123;</span><br><span class="line"> <span class="keyword">let</span> myFunction= <span class="function"><span class="keyword">function</span> <span class="title">fun1</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line"> <span class="built_in">console</span>.log(<span class="string">&quot;This is a second definition&quot;</span>);</span><br><span class="line"> &#125;</span><br><span class="line"> myFunction();</span><br><span class="line">&#125;</span><br><span class="line"> <span class="comment">//myFunction(): ReferenceError: myFunction is not defined.</span></span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="How-do-you-invoke-javascript-code-in-an-iframe-from-parent-page"><a href="#How-do-you-invoke-javascript-code-in-an-iframe-from-parent-page" class="headerlink" title="How do you invoke javascript code in an iframe from parent page"></a>How do you invoke javascript code in an iframe from parent page</h3><p>Initially iFrame needs to be accessed using either <code>document.getElementBy</code> or <code>window.frames</code>. After that <code>contentWindow</code> property of iFrame gives the access for targetFunction</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">document</span>.getElementById(<span class="string">&#x27;targetFrame&#x27;</span>).contentWindow.targetFunction();</span><br><span class="line"><span class="built_in">window</span>.frames[<span class="number">0</span>].frameElement.contentWindow.targetFunction(); <span class="comment">// Accessing iframe this way may not work in latest versions chrome and firefox</span></span><br><span class="line"></span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="How-do-get-the-timezone-offset-from-date"><a href="#How-do-get-the-timezone-offset-from-date" class="headerlink" title="How do get the timezone offset from date"></a>How do get the timezone offset from date</h3><p>You can use the <code>getTimezoneOffset</code> method of the date object. This method returns the time zone difference, in minutes, from current locale (host system settings) to UTC</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> offset = <span class="keyword">new</span> <span class="built_in">Date</span>().getTimezoneOffset();</span><br><span class="line"><span class="built_in">console</span>.log(offset); <span class="comment">// -480</span></span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="How-do-you-load-CSS-and-JS-files-dynamically"><a href="#How-do-you-load-CSS-and-JS-files-dynamically" class="headerlink" title="How do you load CSS and JS files dynamically"></a>How do you load CSS and JS files dynamically</h3><p>You can create both link and script elements in the DOM and append them as child to head tag. Let’s create a function to add script and style resources as below,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">loadAssets</span>(<span class="params">filename, filetype</span>) </span>&#123;</span><br><span class="line">  <span class="keyword">if</span> (filetype == <span class="string">&quot;css&quot;</span>) &#123; <span class="comment">// External CSS file</span></span><br><span class="line">       <span class="keyword">var</span> fileReference = <span class="built_in">document</span>.createElement(<span class="string">&quot;link&quot;</span>)</span><br><span class="line">       fileReference.setAttribute(<span class="string">&quot;rel&quot;</span>, <span class="string">&quot;stylesheet&quot;</span>);</span><br><span class="line">       fileReference.setAttribute(<span class="string">&quot;type&quot;</span>, <span class="string">&quot;text/css&quot;</span>);</span><br><span class="line">       fileReference.setAttribute(<span class="string">&quot;href&quot;</span>, filename);</span><br><span class="line">  &#125; <span class="keyword">else</span> <span class="keyword">if</span> (filetype == <span class="string">&quot;js&quot;</span>) &#123; <span class="comment">// External JavaScript file</span></span><br><span class="line">       <span class="keyword">var</span> fileReference = <span class="built_in">document</span>.createElement(<span class="string">&#x27;script&#x27;</span>);</span><br><span class="line">       fileReference.setAttribute(<span class="string">&quot;type&quot;</span>, <span class="string">&quot;text/javascript&quot;</span>);</span><br><span class="line">       fileReference.setAttribute(<span class="string">&quot;src&quot;</span>, filename);</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="keyword">if</span> (<span class="keyword">typeof</span> fileReference != <span class="string">&quot;undefined&quot;</span>)</span><br><span class="line">       <span class="built_in">document</span>.getElementsByTagName(<span class="string">&quot;head&quot;</span>)[<span class="number">0</span>].appendChild(fileReference)</span><br><span class="line"> &#125;</span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-are-the-different-methods-to-find-HTML-elements-in-DOM"><a href="#What-are-the-different-methods-to-find-HTML-elements-in-DOM" class="headerlink" title="What are the different methods to find HTML elements in DOM"></a>What are the different methods to find HTML elements in DOM</h3><p>If you want to access any element in an HTML page, you need to start with accessing the document object. Later you can use any of the below methods to find the HTML element,</p><ol><li>document.getElementById(id): It finds an element by Id</li><li>document.getElementsByTagName(name): It finds an element by tag name</li><li>document.getElementsByClassName(name): It finds an element by class name</li></ol><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-jQuery"><a href="#What-is-jQuery" class="headerlink" title="What is jQuery"></a>What is jQuery</h3><p>jQuery is a popular cross-browser JavaScript library that provides Document Object Model (DOM) traversal, event handling, animations and AJAX interactions by minimizing the discrepancies across browsers. It is widely famous with its philosophy of “Write less, do more”. For example, you can display welcome message on the page load using jQuery as below,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">$(<span class="built_in">document</span>).ready(<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123; <span class="comment">// It selects the document and apply the function on page load</span></span><br><span class="line">    alert(<span class="string">&#x27;Welcome to jQuery world&#x27;</span>);</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure><p><strong>Note:</strong> You can download it from jquery’s official site or install it from CDNs, like google.</p><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-V8-JavaScript-engine"><a href="#What-is-V8-JavaScript-engine" class="headerlink" title="What is V8 JavaScript engine"></a>What is V8 JavaScript engine</h3><p>V8 is an open source high-performance JavaScript engine used by the Google Chrome browser, written in C++. It is also being used in the node.js project. It implements ECMAScript and WebAssembly, and runs on Windows 7 or later, macOS 10.12+, and Linux systems that use x64, IA-32, ARM, or MIPS processors.<br><strong>Note:</strong> It can run standalone, or can be embedded into any C++ application.</p><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="Why-do-we-call-javascript-as-dynamic-language"><a href="#Why-do-we-call-javascript-as-dynamic-language" class="headerlink" title="Why do we call javascript as dynamic language"></a>Why do we call javascript as dynamic language</h3><p>JavaScript is a loosely typed or a dynamic language because variables in JavaScript are not directly associated with any particular value type, and any variable can be assigned/reassigned with values of all types.</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> age = <span class="number">50</span>;    <span class="comment">// age is a number now</span></span><br><span class="line">age  = <span class="string">&#x27;old&#x27;</span>; <span class="comment">// age is a string now</span></span><br><span class="line">age  = <span class="literal">true</span>;  <span class="comment">// age is a boolean</span></span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-a-void-operator"><a href="#What-is-a-void-operator" class="headerlink" title="What is a void operator"></a>What is a void operator</h3><p>The <code>void</code> operator evaluates the given expression and then returns undefined(i.e, without returning value). The syntax would be as below,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">void</span> (expression)</span><br><span class="line"><span class="keyword">void</span> expression</span><br></pre></td></tr></table></figure><p>Let’s display a message without any redirections or reloads</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">&lt;a href=<span class="string">&quot;javascript:void(alert(&#x27;Welcome to JS world&#x27;))&quot;</span>&gt;Click here to see a message&lt;/a&gt;</span><br></pre></td></tr></table></figure><p><strong>Note:</strong> This operator is often used to obtain the undefined primitive value, using “void(0)”.</p><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="How-to-set-the-cursor-to-wait"><a href="#How-to-set-the-cursor-to-wait" class="headerlink" title="How to set the cursor to wait"></a>How to set the cursor to wait</h3><p>The cursor can be set to wait in JavaScript by using the property “cursor”. Let’s perform this behavior on page load using the below function.</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">myFunction</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line"><span class="built_in">window</span>.document.body.style.cursor = <span class="string">&quot;wait&quot;</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>and this function invoked on page load</p><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">body</span> <span class="attr">onload</span>=<span class="string">&quot;myFunction()&quot;</span>&gt;</span></span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="How-do-you-create-an-infinite-loop"><a href="#How-do-you-create-an-infinite-loop" class="headerlink" title="How do you create an infinite loop"></a>How do you create an infinite loop</h3><p>You can create infinite loops using for and while loops without using any expressions. The for loop construct or syntax is better approach in terms of ESLint and code optimizer tools,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">for</span> (;;) &#123;&#125;</span><br><span class="line"><span class="keyword">while</span>(<span class="literal">true</span>) &#123;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="Why-do-you-need-to-avoid-with-statement"><a href="#Why-do-you-need-to-avoid-with-statement" class="headerlink" title="Why do you need to avoid with statement"></a>Why do you need to avoid with statement</h3><p>JavaScript’s with statement was intended to provide a shorthand for writing recurring accesses to objects. So it can help reduce file size by reducing the need to repeat a lengthy object reference without performance penalty. Let’s take an example where it is used to avoid redundancy when accessing an object several times.</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">a.b.c.greeting   = <span class="string">&#x27;welcome&#x27;</span>;</span><br><span class="line">a.b.c.age = <span class="number">32</span>;</span><br></pre></td></tr></table></figure><p>Using <code>with</code> it turns this into:</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="title">with</span>(<span class="params">a.b.c</span>)</span> &#123;</span><br><span class="line">        greeting   = <span class="string">&quot;welcome&quot;</span>;</span><br><span class="line">        age = <span class="number">32</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>But this <code>with</code> statement creates performance problems since one cannot predict whether an argument will refer to a real variable or to a property inside the with argument.</p><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-the-output-of-below-for-loops"><a href="#What-is-the-output-of-below-for-loops" class="headerlink" title="What is the output of below for loops"></a>What is the output of below for loops</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>; i &lt; <span class="number">4</span>; i++) &#123; <span class="comment">// global scope</span></span><br><span class="line">  <span class="built_in">setTimeout</span>(<span class="function">() =&gt;</span> <span class="built_in">console</span>.log(i));</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">for</span> (<span class="keyword">let</span> i = <span class="number">0</span>; i &lt; <span class="number">4</span>; i++) &#123; <span class="comment">// block scope</span></span><br><span class="line">  <span class="built_in">setTimeout</span>(<span class="function">() =&gt;</span> <span class="built_in">console</span>.log(i));</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>The output of the above for loops is 4 4 4 4 and 0 1 2 3<br><strong>Explanation:</strong> Due to the event queue/loop of javascript, the <code>setTimeout</code> callback function is called after the loop has been executed. Since the variable i is declared with the <code>var</code> keyword it became a global variable and the value was equal to 4 using iteration when the time setTimeout function is invoked. Hence, the output of the first loop is <code>4 4 4 4</code>. Whereas in the second loop, the variable i is declared as the <code>let</code> keyword it becomes a block scoped variable and it holds a new value(0, 1 ,2 3) for each iteration. Hence, the output of the first loop is <code>0 1 2 3</code>.</p><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="List-down-some-of-the-features-of-ES6"><a href="#List-down-some-of-the-features-of-ES6" class="headerlink" title="List down some of the features of ES6"></a>List down some of the features of ES6</h3><p>Below are the list of some new features of ES6,</p><ol><li>Support for constants or immutable variables</li><li>Block-scope support for variables, constants and functions</li><li>Arrow functions</li><li>Default parameters</li><li>Rest and Spread Parameters</li><li>Template Literals</li><li>Multi-line Strings</li><li>Destructuring Assignment</li><li>Enhanced Object Literals</li><li>Promises</li><li>Classes</li><li>Modules</li></ol><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-ES6"><a href="#What-is-ES6" class="headerlink" title="What is ES6"></a>What is ES6</h3><p>ES6 is the sixth edition of the javascript language and it was released in June 2015. It was initially known as ECMAScript 6 (ES6) and later renamed to ECMAScript 2015. Almost all the modern browsers support ES6 but for the old browsers there are many transpilers, like Babel.js etc.</p><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="Can-I-redeclare-let-and-const-variables"><a href="#Can-I-redeclare-let-and-const-variables" class="headerlink" title="Can I redeclare let and const variables"></a>Can I redeclare let and const variables</h3><p>No, you cannot redeclare let and const variables. If you do, it throws below error</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">Uncaught SyntaxError: Identifier <span class="string">&#x27;someVariable&#x27;</span> has already been declared</span><br></pre></td></tr></table></figure><p><strong>Explanation:</strong> The variable declaration with <code>var</code> keyword refers to a function scope and the variable is treated as if it were declared at the top of the enclosing scope due to hoisting feature. So all the multiple declarations contributing to the same hoisted variable without any error. Let’s take an example of re-declaring variables in the same scope for both var and let/const variables.</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> name = <span class="string">&#x27;John&#x27;</span>;</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">myFunc</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="keyword">var</span> name = <span class="string">&#x27;Nick&#x27;</span>;</span><br><span class="line">    <span class="keyword">var</span> name = <span class="string">&#x27;Abraham&#x27;</span>; <span class="comment">// Re-assigned in the same function block</span></span><br><span class="line">    alert(name); <span class="comment">// Abraham</span></span><br><span class="line">&#125;</span><br><span class="line">myFunc();</span><br><span class="line">alert(name); <span class="comment">// John</span></span><br></pre></td></tr></table></figure><p>The block-scoped multi-declaration throws syntax error,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> name = <span class="string">&#x27;John&#x27;</span>;</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">myFunc</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="keyword">let</span> name = <span class="string">&#x27;Nick&#x27;</span>;</span><br><span class="line">    <span class="keyword">let</span> name = <span class="string">&#x27;Abraham&#x27;</span>; <span class="comment">// Uncaught SyntaxError: Identifier &#x27;name&#x27; has already been declared</span></span><br><span class="line">    alert(name);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">myFunc();</span><br><span class="line">alert(name);</span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="Is-const-variable-makes-the-value-immutable"><a href="#Is-const-variable-makes-the-value-immutable" class="headerlink" title="Is const variable makes the value immutable"></a>Is const variable makes the value immutable</h3><p>No, the const variable doesn’t make the value immutable. But it disallows subsequent assignments(i.e, You can declare with assignment but can’t assign another value later)</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> userList = [];</span><br><span class="line">userList.push(<span class="string">&#x27;John&#x27;</span>); <span class="comment">// Can mutate even though it can&#x27;t re-assign</span></span><br><span class="line"><span class="built_in">console</span>.log(userList); <span class="comment">// [&#x27;John&#x27;]</span></span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-are-default-parameters"><a href="#What-are-default-parameters" class="headerlink" title="What are default parameters"></a>What are default parameters</h3><p>In E5, we need to depend on logical OR operators to handle default values of function parameters. Whereas in ES6, Default function parameters feature allows parameters to be initialized with default values if no value or undefined is passed. Let’s compare the behavior with an examples,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//ES5</span></span><br><span class="line"><span class="keyword">var</span> calculateArea = <span class="function"><span class="keyword">function</span>(<span class="params">height, width</span>) </span>&#123;</span><br><span class="line">   height =  height || <span class="number">50</span>;</span><br><span class="line">   width = width || <span class="number">60</span>;</span><br><span class="line"></span><br><span class="line">   <span class="keyword">return</span> width * height;</span><br><span class="line">&#125;</span><br><span class="line"><span class="built_in">console</span>.log(calculateArea()); <span class="comment">//300</span></span><br></pre></td></tr></table></figure><p>The default parameters makes the initialization more simpler,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//ES6</span></span><br><span class="line"><span class="keyword">var</span> calculateArea = <span class="function"><span class="keyword">function</span>(<span class="params">height = <span class="number">50</span>, width = <span class="number">60</span></span>) </span>&#123;</span><br><span class="line">   <span class="keyword">return</span> width * height;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="built_in">console</span>.log(calculateArea()); <span class="comment">//300</span></span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-are-template-literals"><a href="#What-are-template-literals" class="headerlink" title="What are template literals"></a>What are template literals</h3><p>Template literals or template strings are string literals allowing embedded expressions. These are enclosed by the back-tick (`) character instead of double or single quotes.<br>In E6, this feature enables using dynamic expressions as below,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> greeting = <span class="string">`Welcome to JS World, Mr. <span class="subst">$&#123;firstName&#125;</span> <span class="subst">$&#123;lastName&#125;</span>.`</span></span><br></pre></td></tr></table></figure><p>In ES5, you need break string like below,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> greeting = <span class="string">&#x27;Welcome to JS World, Mr. &#x27;</span> + firstName + <span class="string">&#x27; &#x27;</span> + lastName.<span class="string">`</span></span><br></pre></td></tr></table></figure><p><strong>Note:</strong> You can use multi-line strings and string interpolation features with template literals.</p><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="How-do-you-write-multi-line-strings-in-template-literals"><a href="#How-do-you-write-multi-line-strings-in-template-literals" class="headerlink" title="How do you write multi-line strings in template literals"></a>How do you write multi-line strings in template literals</h3><p>In ES5, you would have to use newline escape characters(‘\n’) and concatenation symbols(+) in order to get multi-line strings.</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">console</span>.log(<span class="string">&#x27;This is string sentence 1\n&#x27;</span> +</span><br><span class="line"><span class="string">&#x27;This is string sentence 2&#x27;</span>);</span><br></pre></td></tr></table></figure><p>Whereas in ES6, You don’t need to mention any newline sequence character,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">console</span>.log(<span class="string">`This is string sentence</span></span><br><span class="line"><span class="string">&#x27;This is string sentence 2`</span>);</span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-are-nesting-templates"><a href="#What-are-nesting-templates" class="headerlink" title="What are nesting templates"></a>What are nesting templates</h3><p>The nesting template is a feature supported within template literals syntax to allow inner backticks inside a placeholder ${ } within the template. For example, the below nesting template is used to display the icons based on user permissions whereas outer template checks for platform type,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> iconStyles = <span class="string">`icon <span class="subst">$&#123; isMobilePlatform() ? <span class="string">&#x27;&#x27;</span> :</span></span></span><br><span class="line"><span class="string"><span class="subst"> <span class="string">`icon-<span class="subst">$&#123;user.isAuthorized ? <span class="string">&#x27;submit&#x27;</span> : <span class="string">&#x27;disabled&#x27;</span>&#125;</span>`</span> &#125;</span>`</span>;</span><br></pre></td></tr></table></figure><p>You can write the above use case without nesting template features as well. However, the nesting template feature is more compact and readable.</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//Without nesting templates</span></span><br><span class="line"> <span class="keyword">const</span> iconStyles = <span class="string">`icon <span class="subst">$&#123; isMobilePlatform() ? <span class="string">&#x27;&#x27;</span> :</span></span></span><br><span class="line"><span class="string"><span class="subst">  (user.isAuthorized ? <span class="string">&#x27;icon-submit&#x27;</span> : <span class="string">&#x27;icon-disabled&#x27;</span>&#125;</span>`</span>;</span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-are-tagged-templates"><a href="#What-are-tagged-templates" class="headerlink" title="What are tagged templates"></a>What are tagged templates</h3><p>Tagged templates are the advanced form of templates in which tags allow you to parse template literals with a function. The tag function accepts the first parameter as an array of strings and remaining parameters as expressions. This function can also return manipulated strings based on parameters. Let’s see the usage of this tagged template behavior of an IT professional skill set in an organization,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> user1 = <span class="string">&#x27;John&#x27;</span>;</span><br><span class="line"><span class="keyword">var</span> skill1 = <span class="string">&#x27;JavaScript&#x27;</span>;</span><br><span class="line"><span class="keyword">var</span> experience1 = <span class="number">15</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> user2 = <span class="string">&#x27;Kane&#x27;</span>;</span><br><span class="line"><span class="keyword">var</span> skill2 = <span class="string">&#x27;JavaScript&#x27;</span>;</span><br><span class="line"><span class="keyword">var</span> experience2 = <span class="number">5</span>;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">myInfoTag</span>(<span class="params">strings, userExp, experienceExp, skillExp</span>) </span>&#123;</span><br><span class="line">  <span class="keyword">var</span> str0 = strings[<span class="number">0</span>]; <span class="comment">// &quot;Mr/Ms. &quot;</span></span><br><span class="line">  <span class="keyword">var</span> str1 = strings[<span class="number">1</span>]; <span class="comment">// &quot; is a/an &quot;</span></span><br><span class="line">  <span class="keyword">var</span> str2 = strings[<span class="number">2</span>]; <span class="comment">// &quot;in&quot;</span></span><br><span class="line"></span><br><span class="line">  <span class="keyword">var</span> expertiseStr;</span><br><span class="line">  <span class="keyword">if</span> (experienceExp &gt; <span class="number">10</span>)&#123;</span><br><span class="line">    expertiseStr = <span class="string">&#x27;expert developer&#x27;</span>;</span><br><span class="line">  &#125; <span class="keyword">else</span> <span class="keyword">if</span>(skillExp &gt; <span class="number">5</span> &amp;&amp; skillExp &lt;= <span class="number">10</span>) &#123;</span><br><span class="line">    expertiseStr = <span class="string">&#x27;senior developer&#x27;</span>;</span><br><span class="line">  &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">    expertiseStr = <span class="string">&#x27;junior developer&#x27;</span>;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">return</span> $&#123;str0&#125;$&#123;userExp&#125;$&#123;str1&#125;$&#123;expertiseStr&#125;$&#123;str2&#125;$&#123;skillExp&#125;;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> output1 = myInfoTag<span class="string">`Mr/Ms. <span class="subst">$&#123; user1 &#125;</span> is a/an <span class="subst">$&#123; experience1 &#125;</span> in <span class="subst">$&#123;skill1&#125;</span>`</span>;</span><br><span class="line"><span class="keyword">var</span> output2 = myInfoTag<span class="string">`Mr/Ms. <span class="subst">$&#123; user2 &#125;</span> is a/an <span class="subst">$&#123; experience2 &#125;</span> in <span class="subst">$&#123;skill2&#125;</span>`</span>;</span><br><span class="line"></span><br><span class="line"><span class="built_in">console</span>.log(output1);<span class="comment">// Mr/Ms. John is a/an expert developer in JavaScript</span></span><br><span class="line"><span class="built_in">console</span>.log(output2);<span class="comment">// Mr/Ms. Kane is a/an junior developer in JavaScript</span></span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-are-raw-strings"><a href="#What-are-raw-strings" class="headerlink" title="What are raw strings"></a>What are raw strings</h3><p>ES6 provides a raw strings feature using the <code>String.raw()</code> method which is used to get the raw string form of template strings. This feature allows you to access the raw strings as they were entered, without processing escape sequences. For example, the usage would be as below,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> calculationString = <span class="built_in">String</span>.raw <span class="string">`The sum of numbers is \n<span class="subst">$&#123;<span class="number">1</span>+<span class="number">2</span>+<span class="number">3</span>+<span class="number">4</span>&#125;</span>!`</span>;</span><br><span class="line"><span class="built_in">console</span>.log(calculationString); <span class="comment">// The sum of numbers is 10</span></span><br></pre></td></tr></table></figure><p>If you don’t use raw strings, the newline character sequence will be processed by displaying the output in multiple lines</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> calculationString = <span class="string">`The sum of numbers is \n<span class="subst">$&#123;<span class="number">1</span>+<span class="number">2</span>+<span class="number">3</span>+<span class="number">4</span>&#125;</span>!`</span>;</span><br><span class="line"><span class="built_in">console</span>.log(calculationString);</span><br><span class="line"><span class="comment">// The sum of numbers is</span></span><br><span class="line"><span class="comment">// 10</span></span><br></pre></td></tr></table></figure><p>Also, the raw property is available on the first argument to the tag function</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">tag</span>(<span class="params">strings</span>) </span>&#123;</span><br><span class="line">   <span class="built_in">console</span>.log(strings.raw[<span class="number">0</span>]);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-destructuring-assignment"><a href="#What-is-destructuring-assignment" class="headerlink" title="What is destructuring assignment"></a>What is destructuring assignment</h3><p>The destructuring assignment is a JavaScript expression that makes it possible to unpack values from arrays or properties from objects into distinct variables.<br>Let’s get the month values from an array using destructuring assignment</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> [one, two, three] = [<span class="string">&#x27;JAN&#x27;</span>, <span class="string">&#x27;FEB&#x27;</span>, <span class="string">&#x27;MARCH&#x27;</span>];</span><br><span class="line"></span><br><span class="line"><span class="built_in">console</span>.log(one); <span class="comment">// &quot;JAN&quot;</span></span><br><span class="line"><span class="built_in">console</span>.log(two); <span class="comment">// &quot;FEB&quot;</span></span><br><span class="line"><span class="built_in">console</span>.log(three); <span class="comment">// &quot;MARCH&quot;</span></span><br></pre></td></tr></table></figure><p>and you can get user properties of an object using destructuring assignment,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> &#123;name, age&#125; = &#123;<span class="attr">name</span>: <span class="string">&#x27;John&#x27;</span>, <span class="attr">age</span>: <span class="number">32</span>&#125;;</span><br><span class="line"></span><br><span class="line"><span class="built_in">console</span>.log(name); <span class="comment">// John</span></span><br><span class="line"><span class="built_in">console</span>.log(age); <span class="comment">// 32</span></span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-are-default-values-in-destructuring-assignment"><a href="#What-are-default-values-in-destructuring-assignment" class="headerlink" title="What are default values in destructuring assignment"></a>What are default values in destructuring assignment</h3><p>A variable can be assigned a default value when the value unpacked from the array or object is undefined during destructuring assignment. It helps to avoid setting default values separately for each assignment. Let’s take an example for both arrays and object use cases,</p><p><strong>Arrays destructuring:</strong></p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> x, y, z;</span><br><span class="line"></span><br><span class="line">[x=<span class="number">2</span>, y=<span class="number">4</span>, z=<span class="number">6</span>] = [<span class="number">10</span>];</span><br><span class="line"><span class="built_in">console</span>.log(x); <span class="comment">// 10</span></span><br><span class="line"><span class="built_in">console</span>.log(y); <span class="comment">// 4</span></span><br><span class="line"><span class="built_in">console</span>.log(z); <span class="comment">// 6</span></span><br></pre></td></tr></table></figure><p><strong>Objects destructuring:</strong></p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> &#123;x=<span class="number">2</span>, y=<span class="number">4</span>, z=<span class="number">6</span>&#125; = &#123;<span class="attr">x</span>: <span class="number">10</span>&#125;;</span><br><span class="line"></span><br><span class="line"><span class="built_in">console</span>.log(x); <span class="comment">// 10</span></span><br><span class="line"><span class="built_in">console</span>.log(y); <span class="comment">// 4</span></span><br><span class="line"><span class="built_in">console</span>.log(z); <span class="comment">// 6</span></span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="How-do-you-swap-variables-in-destructuring-assignment"><a href="#How-do-you-swap-variables-in-destructuring-assignment" class="headerlink" title="How do you swap variables in destructuring assignment"></a>How do you swap variables in destructuring assignment</h3><p>If you don’t use destructuring assignment, swapping two values requires a temporary variable. Whereas using a destructuring feature, two variable values can be swapped in one destructuring expression. Let’s swap two number variables in array destructuring assignment,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> x = <span class="number">10</span>, y = <span class="number">20</span>;</span><br><span class="line"></span><br><span class="line">[x, y] = [y, x];</span><br><span class="line"><span class="built_in">console</span>.log(x); <span class="comment">// 20</span></span><br><span class="line"><span class="built_in">console</span>.log(y); <span class="comment">// 10</span></span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-are-enhanced-object-literals"><a href="#What-are-enhanced-object-literals" class="headerlink" title="What are enhanced object literals"></a>What are enhanced object literals</h3><p>Object literals make it easy to quickly create objects with properties inside the curly braces. For example, it provides shorter syntax for common object property definition as below.</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//ES6</span></span><br><span class="line"><span class="keyword">var</span> x = <span class="number">10</span>, y = <span class="number">20</span></span><br><span class="line">obj = &#123; x, y &#125;</span><br><span class="line"><span class="built_in">console</span>.log(obj); <span class="comment">// &#123;x: 10, y:20&#125;</span></span><br><span class="line"><span class="comment">//ES5</span></span><br><span class="line"><span class="keyword">var</span> x = <span class="number">10</span>, y = <span class="number">20</span></span><br><span class="line">obj = &#123; <span class="attr">x</span> : x, <span class="attr">y</span> : y&#125;</span><br><span class="line"><span class="built_in">console</span>.log(obj); <span class="comment">// &#123;x: 10, y:20&#125;</span></span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-are-dynamic-imports"><a href="#What-are-dynamic-imports" class="headerlink" title="What are dynamic imports"></a>What are dynamic imports</h3><p>The dynamic imports using <code>import()</code> function syntax allows us to load modules on demand by using promises or the async/await syntax. Currently this feature is in <a target="_blank" rel="noopener" href="https://github.com/tc39/proposal-dynamic-import">stage4 proposal</a>. The main advantage of dynamic imports is reduction of our bundle’s sizes, the size/payload response of our requests and overall improvements in the user experience.<br>The syntax of dynamic imports would be as below,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span>(<span class="string">&#x27;./Module&#x27;</span>).then(<span class="function"><span class="params">Module</span> =&gt;</span> Module.method());</span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-are-the-use-cases-for-dynamic-imports"><a href="#What-are-the-use-cases-for-dynamic-imports" class="headerlink" title="What are the use cases for dynamic imports"></a>What are the use cases for dynamic imports</h3><p>Below are some of the use cases of using dynamic imports over static imports,</p><ol><li>Import a module on-demand or conditionally. For example, if you want to load a polyfill on legacy browser</li></ol><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">if</span> (isLegacyBrowser()) &#123;</span><br><span class="line">    <span class="keyword">import</span>(···)</span><br><span class="line">    .then(···);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><ol><li>Compute the module specifier at runtime. For example, you can use it for internationalization.</li></ol><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span>(<span class="string">`messages_<span class="subst">$&#123;getLocale()&#125;</span>.js`</span>).then(···);</span><br></pre></td></tr></table></figure><ol><li>Import a module from within a regular script instead a module.</li></ol><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-are-typed-arrays"><a href="#What-are-typed-arrays" class="headerlink" title="What are typed arrays"></a>What are typed arrays</h3><p>Typed arrays are array-like objects from ECMAScript 6 API for handling binary data. JavaScript provides 8 Typed array types,</p><ol><li>Int8Array: An array of 8-bit signed integers</li><li>Int16Array: An array of 16-bit signed integers</li><li>Int32Array: An array of 32-bit signed integers</li><li>Uint8Array: An array of 8-bit unsigned integers</li><li>Uint16Array: An array of 16-bit unsigned integers</li><li>Uint32Array: An array of 32-bit unsigned integers</li><li>Float32Array: An array of 32-bit floating point numbers</li><li>Float64Array: An array of 64-bit floating point numbers</li></ol><p>For example, you can create an array of 8-bit signed integers as below</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> a = <span class="keyword">new</span> <span class="built_in">Int8Array</span>();</span><br><span class="line"><span class="comment">// You can pre-allocate n bytes</span></span><br><span class="line"><span class="keyword">const</span> bytes = <span class="number">1024</span></span><br><span class="line"><span class="keyword">const</span> a = <span class="keyword">new</span> <span class="built_in">Int8Array</span>(bytes)</span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-are-the-advantages-of-module-loaders"><a href="#What-are-the-advantages-of-module-loaders" class="headerlink" title="What are the advantages of module loaders"></a>What are the advantages of module loaders</h3><p>The module loaders provides the below features,</p><ol><li>Dynamic loading</li><li>State isolation</li><li>Global namespace isolation</li><li>Compilation hooks</li><li>Nested virtualization</li></ol><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-collation"><a href="#What-is-collation" class="headerlink" title="What is collation"></a>What is collation</h3><p>Collation is used for sorting a set of strings and searching within a set of strings. It is parameterized by locale and aware of Unicode. Let’s take comparison and sorting features,</p><ol><li><strong>Comparison:</strong></li></ol><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> list = [ <span class="string">&quot;ä&quot;</span>, <span class="string">&quot;a&quot;</span>, <span class="string">&quot;z&quot;</span> ]; <span class="comment">// In German,  &quot;ä&quot; sorts with &quot;a&quot; Whereas in Swedish, &quot;ä&quot; sorts after &quot;z&quot;</span></span><br><span class="line"><span class="keyword">var</span> l10nDE = <span class="keyword">new</span> <span class="built_in">Intl</span>.Collator(<span class="string">&quot;de&quot;</span>);</span><br><span class="line"><span class="keyword">var</span> l10nSV = <span class="keyword">new</span> <span class="built_in">Intl</span>.Collator(<span class="string">&quot;sv&quot;</span>);</span><br><span class="line"><span class="built_in">console</span>.log(l10nDE.compare(<span class="string">&quot;ä&quot;</span>, <span class="string">&quot;z&quot;</span>) === -<span class="number">1</span>); <span class="comment">// true</span></span><br><span class="line"><span class="built_in">console</span>.log(l10nSV.compare(<span class="string">&quot;ä&quot;</span>, <span class="string">&quot;z&quot;</span>) === +<span class="number">1</span>); <span class="comment">// true</span></span><br></pre></td></tr></table></figure><ol><li><strong>Sorting:</strong></li></ol><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> list = [ <span class="string">&quot;ä&quot;</span>, <span class="string">&quot;a&quot;</span>, <span class="string">&quot;z&quot;</span> ]; <span class="comment">// In German,  &quot;ä&quot; sorts with &quot;a&quot; Whereas in Swedish, &quot;ä&quot; sorts after &quot;z&quot;</span></span><br><span class="line"><span class="keyword">var</span> l10nDE = <span class="keyword">new</span> <span class="built_in">Intl</span>.Collator(<span class="string">&quot;de&quot;</span>);</span><br><span class="line"><span class="keyword">var</span> l10nSV = <span class="keyword">new</span> <span class="built_in">Intl</span>.Collator(<span class="string">&quot;sv&quot;</span>);</span><br><span class="line"><span class="built_in">console</span>.log(list.sort(l10nDE.compare)) <span class="comment">// [ &quot;a&quot;, &quot;ä&quot;, &quot;z&quot; ]</span></span><br><span class="line"><span class="built_in">console</span>.log(list.sort(l10nSV.compare)) <span class="comment">// [ &quot;a&quot;, &quot;z&quot;, &quot;ä&quot; ]</span></span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-for…of-statement"><a href="#What-is-for…of-statement" class="headerlink" title="What is for…of statement"></a>What is for…of statement</h3><p>The for…of statement creates a loop iterating over iterable objects or elements such as built-in String, Array, Array-like objects (like arguments or NodeList), TypedArray, Map, Set, and user-defined iterables. The basic usage of for…of statement on arrays would be as below,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> arrayIterable = [<span class="number">10</span>, <span class="number">20</span>, <span class="number">30</span>, <span class="number">40</span>, <span class="number">50</span>];</span><br><span class="line"></span><br><span class="line"><span class="keyword">for</span> (<span class="keyword">let</span> value <span class="keyword">of</span> arrayIterable) &#123;</span><br><span class="line">  value ++;</span><br><span class="line">  <span class="built_in">console</span>.log(value); <span class="comment">// 11 21 31 41 51</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-the-output-of-below-spread-operator-array"><a href="#What-is-the-output-of-below-spread-operator-array" class="headerlink" title="What is the output of below spread operator array"></a>What is the output of below spread operator array</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">[...<span class="string">&#x27;John Resig&#x27;</span>]</span><br></pre></td></tr></table></figure><p>The output of the array is [‘J’, ‘o’, ‘h’, ‘n’, ‘’, ‘R’, ‘e’, ‘s’, ‘i’, ‘g’]<br><strong>Explanation:</strong> The string is an iterable type and the spread operator within an array maps every character of an iterable to one element. Hence, each character of a string becomes an element within an Array.</p><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="Is-PostMessage-secure"><a href="#Is-PostMessage-secure" class="headerlink" title="Is PostMessage secure"></a>Is PostMessage secure</h3><p>Yes, postMessages can be considered very secure as long as the programmer/developer is careful about checking the origin and source of an arriving message. But if you try to send/receive a message without verifying its source will create cross-site scripting attacks.</p><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-are-the-problems-with-postmessage-target-origin-as-wildcard"><a href="#What-are-the-problems-with-postmessage-target-origin-as-wildcard" class="headerlink" title="What are the problems with postmessage target origin as wildcard"></a>What are the problems with postmessage target origin as wildcard</h3><p>The second argument of postMessage method specifies which origin is allowed to receive the message. If you use the wildcard “*” as an argument then any origin is allowed to receive the message. In this case, there is no way for the sender window to know if the target window is at the target origin when sending the message. If the target window has been navigated to another origin, the other origin would receive the data. Hence, this may lead to XSS vulnerabilities.</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">targetWindow.postMessage(message, <span class="string">&#x27;*&#x27;</span>);</span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="How-do-you-avoid-receiving-postMessages-from-attackers"><a href="#How-do-you-avoid-receiving-postMessages-from-attackers" class="headerlink" title="How do you avoid receiving postMessages from attackers"></a>How do you avoid receiving postMessages from attackers</h3><p>Since the listener listens for any message, an attacker can trick the application by sending a message from the attacker’s origin, which gives an impression that the receiver received the message from the actual sender’s window. You can avoid this issue by validating the origin of the message on the receiver’s end using the “message.origin” attribute. For examples, let’s check the sender’s origin <a target="_blank" rel="noopener" href="http://www.some-sender.com/">http://www.some-sender.com</a> on receiver side <a href="www.some-receiver.com">www.some-receiver.com</a>,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//Listener on http://www.some-receiver.com/</span></span><br><span class="line"><span class="built_in">window</span>.addEventListener(<span class="string">&quot;message&quot;</span>, <span class="function"><span class="keyword">function</span>(<span class="params">message</span>)</span>&#123;</span><br><span class="line">    <span class="keyword">if</span>(<span class="regexp">/^http:/</span><span class="regexp">/www\.some-sender\.com$/</span>.test(message.origin))&#123;</span><br><span class="line">         <span class="built_in">console</span>.log(<span class="string">&#x27;You received the data from valid sender&#x27;</span>, message.data);</span><br><span class="line">   &#125;</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="Can-I-avoid-using-postMessages-completely"><a href="#Can-I-avoid-using-postMessages-completely" class="headerlink" title="Can I avoid using postMessages completely"></a>Can I avoid using postMessages completely</h3><p>You cannot avoid using postMessages completely(or 100%). Even though your application doesn’t use postMessage considering the risks, a lot of third party scripts use postMessage to communicate with the third party service. So your application might be using postMessage without your knowledge.</p><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="Is-postMessages-synchronous"><a href="#Is-postMessages-synchronous" class="headerlink" title="Is postMessages synchronous"></a>Is postMessages synchronous</h3><p>The postMessages are synchronous in IE8 browser but they are asynchronous in IE9 and all other modern browsers (i.e, IE9+, Firefox, Chrome, Safari).Due to this asynchronous behaviour, we use a callback mechanism when the postMessage is returned.</p><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-paradigm-is-Javascript"><a href="#What-paradigm-is-Javascript" class="headerlink" title="What paradigm is Javascript"></a>What paradigm is Javascript</h3><p>JavaScript is a multi-paradigm language, supporting imperative/procedural programming, Object-Oriented Programming and functional programming. JavaScript supports Object-Oriented Programming with prototypical inheritance.</p><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-the-difference-between-internal-and-external-javascript"><a href="#What-is-the-difference-between-internal-and-external-javascript" class="headerlink" title="What is the difference between internal and external javascript"></a>What is the difference between internal and external javascript</h3><p><strong>Internal JavaScript:</strong> It is the source code within the script tag.<br><strong>External JavaScript:</strong> The source code is stored in an external file(stored with .js extension) and referred with in the tag.</p><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="Is-JavaScript-faster-than-server-side-script"><a href="#Is-JavaScript-faster-than-server-side-script" class="headerlink" title="Is JavaScript faster than server side script"></a>Is JavaScript faster than server side script</h3><p>Yes, JavaScript is faster than server side script. Because JavaScript is a client-side script it does not require any web server’s help for its computation or calculation. So JavaScript is always faster than any server-side script like ASP, PHP, etc.</p><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="How-do-you-get-the-status-of-a-checkbox"><a href="#How-do-you-get-the-status-of-a-checkbox" class="headerlink" title="How do you get the status of a checkbox"></a>How do you get the status of a checkbox</h3><p>You can apply the <code>checked</code> property on the selected checkbox in the DOM. If the value is <code>True</code> means the checkbox is checked otherwise it is unchecked. For example, the below HTML checkbox element can be access using javascript as below,</p><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">&quot;checkbox&quot;</span> <span class="attr">name</span>=<span class="string">&quot;checkboxname&quot;</span> <span class="attr">value</span>=<span class="string">&quot;Agree&quot;</span>&gt;</span> Agree the conditions<span class="tag">&lt;<span class="name">br</span>&gt;</span></span><br></pre></td></tr></table></figure><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">console</span>.log(<span class="built_in">document</span>.getElementById(‘checkboxname’).checked); <span class="comment">// true or false</span></span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-the-purpose-of-double-tilde-operator"><a href="#What-is-the-purpose-of-double-tilde-operator" class="headerlink" title="What is the purpose of double tilde operator"></a>What is the purpose of double tilde operator</h3><p>The double tilde operator(~~) is known as double NOT bitwise operator. This operator is going to be a quicker substitute for Math.floor().</p><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="How-do-you-convert-character-to-ASCII-code"><a href="#How-do-you-convert-character-to-ASCII-code" class="headerlink" title="How do you convert character to ASCII code"></a>How do you convert character to ASCII code</h3><p>You can use the <code>String.prototype.charCodeAt()</code> method to convert string characters to ASCII numbers. For example, let’s find ASCII code for the first letter of ‘ABC’ string,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">&quot;ABC&quot;</span>.charCodeAt(<span class="number">0</span>) <span class="comment">// returns 65</span></span><br></pre></td></tr></table></figure><p>Whereas <code>String.fromCharCode()</code> method converts numbers to equal ASCII characters.</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">String</span>.fromCharCode(<span class="number">65</span>,<span class="number">66</span>,<span class="number">67</span>); <span class="comment">// returns &#x27;ABC&#x27;</span></span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-ArrayBuffer"><a href="#What-is-ArrayBuffer" class="headerlink" title="What is ArrayBuffer"></a>What is ArrayBuffer</h3><p>An ArrayBuffer object is used to represent a generic, fixed-length raw binary data buffer. You can create it as below,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> buffer = <span class="keyword">new</span> <span class="built_in">ArrayBuffer</span>(<span class="number">16</span>); <span class="comment">// create a buffer of length 16</span></span><br><span class="line">alert(buffer.byteLength); <span class="comment">// 16</span></span><br></pre></td></tr></table></figure><p>To manipulate an ArrayBuffer, we need to use a “view” object.</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//Create a DataView referring to the buffer</span></span><br><span class="line"> <span class="keyword">let</span> view = <span class="keyword">new</span> <span class="built_in">DataView</span>(buffer);</span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-the-output-of-below-string-expression"><a href="#What-is-the-output-of-below-string-expression" class="headerlink" title="What is the output of below string expression"></a>What is the output of below string expression</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">console</span>.log(<span class="string">&quot;Welcome to JS world&quot;</span>[<span class="number">0</span>])</span><br></pre></td></tr></table></figure><p>The output of the above expression is “W”.<br><strong>Explanation:</strong> The bracket notation with specific index on a string returns the character at a specific location. Hence, it returns the character “W” of the string. Since this is not supported in IE7 and below versions, you may need to use the .charAt() method to get the desired result.</p><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-the-purpose-of-Error-object"><a href="#What-is-the-purpose-of-Error-object" class="headerlink" title="What is the purpose of Error object"></a>What is the purpose of Error object</h3><p>The Error constructor creates an error object and the instances of error objects are thrown when runtime errors occur. The Error object can also be used as a base object for user-defined exceptions. The syntax of error object would be as below,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">new</span> <span class="built_in">Error</span>([message[, fileName[, lineNumber]]])</span><br></pre></td></tr></table></figure><p>You can throw user defined exceptions or errors using Error object in try…catch block as below,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">try</span> &#123;</span><br><span class="line">  <span class="keyword">if</span>(withdraw &gt; balance)</span><br><span class="line">  <span class="keyword">throw</span> <span class="keyword">new</span> <span class="built_in">Error</span>(<span class="string">&quot;Oops! You don&#x27;t have enough balance&quot;</span>);</span><br><span class="line">&#125; <span class="keyword">catch</span> (e) &#123;</span><br><span class="line">  <span class="built_in">console</span>.log(e.name + <span class="string">&#x27;: &#x27;</span> + e.message);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-the-purpose-of-EvalError-object"><a href="#What-is-the-purpose-of-EvalError-object" class="headerlink" title="What is the purpose of EvalError object"></a>What is the purpose of EvalError object</h3><p>The EvalError object indicates an error regarding the global <code>eval()</code> function. Even though this exception is not thrown by JavaScript anymore, the EvalError object remains for compatibility. The syntax of this expression would be as below,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">new</span> <span class="built_in">EvalError</span>([message[, fileName[, lineNumber]]])</span><br></pre></td></tr></table></figure><p>You can throw EvalError with in try…catch block as below,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">try</span> &#123;</span><br><span class="line">  <span class="keyword">throw</span> <span class="keyword">new</span> <span class="built_in">EvalError</span>(<span class="string">&#x27;Eval function error&#x27;</span>, <span class="string">&#x27;someFile.js&#x27;</span>, <span class="number">100</span>);</span><br><span class="line">&#125; <span class="keyword">catch</span> (e) &#123;</span><br><span class="line">  <span class="built_in">console</span>.log(e.message, e.name, e.fileName);              <span class="comment">// &quot;Eval function error&quot;, &quot;EvalError&quot;, &quot;someFile.js&quot;</span></span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-are-the-list-of-cases-error-thrown-from-non-strict-mode-to-strict-mode"><a href="#What-are-the-list-of-cases-error-thrown-from-non-strict-mode-to-strict-mode" class="headerlink" title="What are the list of cases error thrown from non-strict mode to strict mode"></a>What are the list of cases error thrown from non-strict mode to strict mode</h3><p>When you apply ‘use strict’; syntax, some of the below cases will throw a SyntaxError before executing the script</p><ol><li>When you use Octal syntax</li></ol><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> n = <span class="number">022</span>;</span><br></pre></td></tr></table></figure><ol><li>Using <code>with</code> statement</li><li>When you use delete operator on a variable name</li><li>Using eval or arguments as variable or function argument name</li><li>When you use newly reserved keywords</li><li>When you declare a function in a block</li></ol><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">if</span> (someCondition) &#123; <span class="function"><span class="keyword">function</span> <span class="title">f</span>(<span class="params"></span>) </span>&#123;&#125; &#125;</span><br></pre></td></tr></table></figure><p>Hence, the errors from above cases are helpful to avoid errors in development/production environments.</p><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="Is-all-objects-have-prototypes"><a href="#Is-all-objects-have-prototypes" class="headerlink" title="Is all objects have prototypes"></a>Is all objects have prototypes</h3><p>No. All objects have prototypes except for the base object which is created by the user, or an object that is created using the new keyword.</p><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-the-difference-between-a-parameter-and-an-argument"><a href="#What-is-the-difference-between-a-parameter-and-an-argument" class="headerlink" title="What is the difference between a parameter and an argument"></a>What is the difference between a parameter and an argument</h3><p>Parameter is the variable name of a function definition whereas an argument represents the value given to a function when it is invoked. Let’s explain this with a simple function</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">myFunction</span>(<span class="params">parameter1, parameter2, parameter3</span>) </span>&#123;</span><br><span class="line">  <span class="built_in">console</span>.log(<span class="built_in">arguments</span>[<span class="number">0</span>]) <span class="comment">// &quot;argument1&quot;</span></span><br><span class="line">  <span class="built_in">console</span>.log(<span class="built_in">arguments</span>[<span class="number">1</span>]) <span class="comment">// &quot;argument2&quot;</span></span><br><span class="line">  <span class="built_in">console</span>.log(<span class="built_in">arguments</span>[<span class="number">2</span>]) <span class="comment">// &quot;argument3&quot;</span></span><br><span class="line">&#125;</span><br><span class="line">myFunction(<span class="string">&quot;argument1&quot;</span>, <span class="string">&quot;argument2&quot;</span>, <span class="string">&quot;argument3&quot;</span>)</span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-the-purpose-of-some-method-in-arrays"><a href="#What-is-the-purpose-of-some-method-in-arrays" class="headerlink" title="What is the purpose of some method in arrays"></a>What is the purpose of some method in arrays</h3><p>The some() method is used to test whether at least one element in the array passes the test implemented by the provided function. The method returns a boolean value. Let’s take an example to test for any odd elements,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> array = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span> ,<span class="number">7</span>, <span class="number">8</span>, <span class="number">9</span>, <span class="number">10</span>];</span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> odd = element ==&gt; element % <span class="number">2</span> !== <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line"><span class="built_in">console</span>.log(array.some(odd)); <span class="comment">// true (the odd element exists)</span></span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="How-do-you-combine-two-or-more-arrays"><a href="#How-do-you-combine-two-or-more-arrays" class="headerlink" title="How do you combine two or more arrays"></a>How do you combine two or more arrays</h3><p>The concat() method is used to join two or more arrays by returning a new array containing all the elements. The syntax would be as below,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">array1.concat(array2, array3, ..., arrayX)</span><br></pre></td></tr></table></figure><p>Let’s take an example of array’s concatenation with veggies and fruits arrays,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> veggies = [<span class="string">&quot;Tomato&quot;</span>, <span class="string">&quot;Carrot&quot;</span>, <span class="string">&quot;Cabbage&quot;</span>];</span><br><span class="line"><span class="keyword">var</span> fruits = [<span class="string">&quot;Apple&quot;</span>, <span class="string">&quot;Orange&quot;</span>, <span class="string">&quot;Pears&quot;</span>];</span><br><span class="line"><span class="keyword">var</span> veggiesAndFruits = veggies.concat(fruits);</span><br><span class="line"><span class="built_in">console</span>.log(veggiesAndFruits); <span class="comment">// Tomato, Carrot, Cabbage, Apple, Orange, Pears</span></span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-the-difference-between-Shallow-and-Deep-copy"><a href="#What-is-the-difference-between-Shallow-and-Deep-copy" class="headerlink" title="What is the difference between Shallow and Deep copy"></a>What is the difference between Shallow and Deep copy</h3><p>There are two ways to copy an object,</p><p><strong>Shallow Copy:</strong><br>Shallow copy is a bitwise copy of an object. A new object is created that has an exact copy of the values in the original object. If any of the fields of the object are references to other objects, just the reference addresses are copied i.e., only the memory address is copied.</p><p><strong>Example</strong></p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> empDetails = &#123;</span><br><span class="line">  name: <span class="string">&quot;John&quot;</span>, <span class="attr">age</span>: <span class="number">25</span>, <span class="attr">expertise</span>: <span class="string">&quot;Software Developer&quot;</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>to create a duplicate</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> empDetailsShallowCopy = empDetails    <span class="comment">//Shallow copying!</span></span><br></pre></td></tr></table></figure><p>if we change some property value in the duplicate one like this:</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">empDetailsShallowCopy.name = <span class="string">&quot;Johnson&quot;</span></span><br></pre></td></tr></table></figure><p>The above statement will also change the name of <code>empDetails</code>, since we have a shallow copy. That means we’re losing the original data as well.</p><p><strong>Deep copy:</strong><br>A deep copy copies all fields, and makes copies of dynamically allocated memory pointed to by the fields. A deep copy occurs when an object is copied along with the objects to which it refers.</p><p><strong>Example</strong></p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> empDetails = &#123;</span><br><span class="line">  name: <span class="string">&quot;John&quot;</span>, <span class="attr">age</span>: <span class="number">25</span>, <span class="attr">expertise</span>: <span class="string">&quot;Software Developer&quot;</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>Create a deep copy by using the properties from the original object into new variable</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> empDetailsDeepCopy = &#123;</span><br><span class="line">  name: empDetails.name,</span><br><span class="line">  age: empDetails.age,</span><br><span class="line">  expertise: empDetails.expertise</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>Now if you change <code>empDetailsDeepCopy.name</code>, it will only affect <code>empDetailsDeepCopy</code> &amp; not <code>empDetails</code></p><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="How-do-you-create-specific-number-of-copies-of-a-string"><a href="#How-do-you-create-specific-number-of-copies-of-a-string" class="headerlink" title="How do you create specific number of copies of a string"></a>How do you create specific number of copies of a string</h3><p>The <code>repeat()</code> method is used to construct and return a new string which contains the specified number of copies of the string on which it was called, concatenated together. Remember that this method has been added to the ECMAScript 2015 specification.<br>Let’s take an example of Hello string to repeat it 4 times,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">&#x27;Hello&#x27;</span>.repeat(<span class="number">4</span>);  <span class="comment">// &#x27;HelloHelloHelloHello&#x27;</span></span><br></pre></td></tr></table></figure></li><li><h3 id="How-do-you-return-all-matching-strings-against-a-regular-expression"><a href="#How-do-you-return-all-matching-strings-against-a-regular-expression" class="headerlink" title="How do you return all matching strings against a regular expression"></a>How do you return all matching strings against a regular expression</h3><p>The <code>matchAll()</code> method can be used to return an iterator of all results matching a string against a regular expression. For example, the below example returns an array of matching string results against a regular expression,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> regexp = <span class="regexp">/Hello(\d?))/g</span>;</span><br><span class="line"><span class="keyword">let</span> greeting = <span class="string">&#x27;Hello1Hello2Hello3&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> greetingList = [...greeting.matchAll(regexp)];</span><br><span class="line"></span><br><span class="line"><span class="built_in">console</span>.log(greetingList[<span class="number">0</span>]); <span class="comment">//Hello1</span></span><br><span class="line"><span class="built_in">console</span>.log(greetingList[<span class="number">1</span>]); <span class="comment">//Hello2</span></span><br><span class="line"><span class="built_in">console</span>.log(greetingList[<span class="number">2</span>]); <span class="comment">//Hello3</span></span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="How-do-you-trim-a-string-at-the-beginning-or-ending"><a href="#How-do-you-trim-a-string-at-the-beginning-or-ending" class="headerlink" title="How do you trim a string at the beginning or ending"></a>How do you trim a string at the beginning or ending</h3><p>The <code>trim</code> method of string prototype is used to trim on both sides of a string. But if you want to trim especially at the beginning or ending of the string then you can use <code>trimStart/trimLeft</code> and <code>trimEnd/trimRight</code> methods. Let’s see an example of these methods on a greeting message,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> greeting = <span class="string">&#x27;   Hello, Goodmorning!   &#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="built_in">console</span>.log(greeting); <span class="comment">// &quot;   Hello, Goodmorning!   &quot;</span></span><br><span class="line"><span class="built_in">console</span>.log(greeting.trimStart()); <span class="comment">// &quot;Hello, Goodmorning!   &quot;</span></span><br><span class="line"><span class="built_in">console</span>.log(greeting.trimLeft()); <span class="comment">// &quot;Hello, Goodmorning!   &quot;</span></span><br><span class="line"></span><br><span class="line"><span class="built_in">console</span>.log(greeting.trimEnd()); <span class="comment">// &quot;   Hello, Goodmorning!&quot;</span></span><br><span class="line"><span class="built_in">console</span>.log(greeting.trimRight()); <span class="comment">// &quot;   Hello, Goodmorning!&quot;</span></span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-the-output-of-below-console-statement-with-unary-operator"><a href="#What-is-the-output-of-below-console-statement-with-unary-operator" class="headerlink" title="What is the output of below console statement with unary operator"></a>What is the output of below console statement with unary operator</h3><p>Let’s take console statement with unary operator as given below,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">console</span>.log(+ <span class="string">&#x27;Hello&#x27;</span>);</span><br></pre></td></tr></table></figure><p>The output of the above console log statement returns NaN. Because the element is prefixed by the unary operator and the JavaScript interpreter will try to convert that element into a number type. Since the conversion fails, the value of the statement results in NaN value.</p><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="Does-javascript-uses-mixins"><a href="#Does-javascript-uses-mixins" class="headerlink" title="Does javascript uses mixins"></a>Does javascript uses mixins</h3><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-a-thunk-function"><a href="#What-is-a-thunk-function" class="headerlink" title="What is a thunk function"></a>What is a thunk function</h3><p>A thunk is just a function which delays the evaluation of the value. It doesn’t take any arguments but gives the value whenever you invoke the thunk. i.e, It is used not to execute now but it will be sometime in the future. Let’s take a synchronous example,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> add = <span class="function">(<span class="params">x,y</span>) =&gt;</span> x + y;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> thunk = <span class="function">() =&gt;</span> add(<span class="number">2</span>,<span class="number">3</span>);</span><br><span class="line"></span><br><span class="line">thunk() <span class="comment">// 5</span></span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-are-asynchronous-thunks"><a href="#What-are-asynchronous-thunks" class="headerlink" title="What are asynchronous thunks"></a>What are asynchronous thunks</h3><p>The asynchronous thunks are useful to make network requests. Let’s see an example of network requests,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">fetchData</span>(<span class="params">fn</span>)</span>&#123;</span><br><span class="line">  fetch(<span class="string">&#x27;https://jsonplaceholder.typicode.com/todos/1&#x27;</span>)</span><br><span class="line">  .then(<span class="function"><span class="params">response</span> =&gt;</span> response.json())</span><br><span class="line">  .then(<span class="function"><span class="params">json</span> =&gt;</span> fn(json))</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> asyncThunk = <span class="function"><span class="keyword">function</span> (<span class="params"></span>)</span>&#123;</span><br><span class="line">   <span class="keyword">return</span> fetchData(<span class="function"><span class="keyword">function</span> <span class="title">getData</span>(<span class="params">data</span>)</span>&#123;</span><br><span class="line">      <span class="built_in">console</span>.log(data)</span><br><span class="line">  &#125;)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">asyncThunk()</span><br></pre></td></tr></table></figure><p>The <code>getData</code> function won’t be called immediately but it will be invoked only when the data is available from API endpoint. The setTimeout function is also used to make our code asynchronous. The best real time example is redux state management library which uses the asynchronous thunks to delay the actions to dispatch.</p><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-the-output-of-below-function-calls"><a href="#What-is-the-output-of-below-function-calls" class="headerlink" title="What is the output of below function calls"></a>What is the output of below function calls</h3><p><strong>Code snippet:</strong></p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> circle = &#123;</span><br><span class="line">  radius: <span class="number">20</span>,</span><br><span class="line">  <span class="function"><span class="title">diameter</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="built_in">this</span>.radius * <span class="number">2</span>;</span><br><span class="line">  &#125;,</span><br><span class="line">  perimeter: <span class="function">() =&gt;</span> <span class="number">2</span> * <span class="built_in">Math</span>.PI * <span class="built_in">this</span>.radius</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure><p>console.log(circle.diameter());<br>console.log(circle.perimeter());</p><p><strong>Output:</strong></p><p>The output is 40 and NaN. Remember that diameter is a regular function, whereas the value of perimeter is an arrow function. The <code>this</code> keyword of a regular function(i.e, diameter) refers to the surrounding scope which is a class(i.e, Shape object). Whereas this keyword of perimeter function refers to the surrounding scope which is a window object. Since there is no radius property on window objects it returns an undefined value and the multiple of number value returns NaN value.</p><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="How-to-remove-all-line-breaks-from-a-string"><a href="#How-to-remove-all-line-breaks-from-a-string" class="headerlink" title="How to remove all line breaks from a string"></a>How to remove all line breaks from a string</h3><p>The easiest approach is using regular expressions to detect and replace newlines in the string. In this case, we use replace function along with string to replace with, which in our case is an empty string.</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">remove_linebreaks</span>(<span class="params"> <span class="keyword">var</span> message </span>) </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> message.replace( <span class="regexp">/[\r\n]+/gm</span>, <span class="string">&quot;&quot;</span> );</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>In the above expression, g and m are for global and multiline flags.</p><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-the-difference-between-reflow-and-repaint"><a href="#What-is-the-difference-between-reflow-and-repaint" class="headerlink" title="What is the difference between reflow and repaint"></a>What is the difference between reflow and repaint</h3><p>A <em>repaint</em> occurs when changes are made which affect the visibility of an element, but not its layout. Examples of this include outline, visibility, or background color. A <em>reflow</em> involves changes that affect the layout of a portion of the page (or the whole page). Resizing the browser window, changing the font, content changing (such as user typing text), using JavaScript methods involving computed styles, adding or removing elements from the DOM, and changing an element’s classes are a few of the things that can trigger reflow. Reflow of an element causes the subsequent reflow of all child and ancestor elements as well as any elements following it in the DOM.</p><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-happens-with-negating-an-array"><a href="#What-happens-with-negating-an-array" class="headerlink" title="What happens with negating an array"></a>What happens with negating an array</h3><p>Negating an array with <code>!</code> character will coerce the array into a boolean. Since Arrays are considered to be truthy So negating it will return <code>false</code>.</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">console</span>.log(![]); <span class="comment">// false</span></span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-happens-if-we-add-two-arrays"><a href="#What-happens-if-we-add-two-arrays" class="headerlink" title="What happens if we add two arrays"></a>What happens if we add two arrays</h3><p>If you add two arrays together, it will convert them both to strings and concatenate them. For example, the result of adding arrays would be as below,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">console</span>.log([<span class="string">&#x27;a&#x27;</span>] + [<span class="string">&#x27;b&#x27;</span>]);  <span class="comment">// &quot;ab&quot;</span></span><br><span class="line"><span class="built_in">console</span>.log([] + []); <span class="comment">// &quot;&quot;</span></span><br><span class="line"><span class="built_in">console</span>.log(![] + []); <span class="comment">// &quot;false&quot;, because ![] returns false.</span></span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-the-output-of-prepend-additive-operator-on-falsy-values"><a href="#What-is-the-output-of-prepend-additive-operator-on-falsy-values" class="headerlink" title="What is the output of prepend additive operator on falsy values"></a>What is the output of prepend additive operator on falsy values</h3><p>If you prepend the additive(+) operator on falsy values(null, undefined, NaN, false, “”), the falsy value converts to a number value zero. Let’s display them on browser console as below,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">console</span>.log(+<span class="literal">null</span>); <span class="comment">// 0</span></span><br><span class="line"><span class="built_in">console</span>.log(+<span class="literal">undefined</span>);<span class="comment">// NaN</span></span><br><span class="line"><span class="built_in">console</span>.log(+<span class="literal">false</span>); <span class="comment">// 0</span></span><br><span class="line"><span class="built_in">console</span>.log(+<span class="literal">NaN</span>); <span class="comment">// NaN</span></span><br><span class="line"><span class="built_in">console</span>.log(+<span class="string">&quot;&quot;</span>); <span class="comment">// 0</span></span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="How-do-you-create-self-string-using-special-characters"><a href="#How-do-you-create-self-string-using-special-characters" class="headerlink" title="How do you create self string using special characters"></a>How do you create self string using special characters</h3><p>The self string can be formed with the combination of <code>[]()!+</code> characters. You need to remember the below conventions to achieve this pattern.</p><ol><li>Since Arrays are truthful values, negating the arrays will produce false: ![] === false</li><li>As per JavaScript coercion rules, the addition of arrays together will toString them: [] + [] === “”</li><li>Prepend an array with + operator will convert an array to false, the negation will make it true and finally converting the result will produce value ‘1’: +(!(+[])) === 1</li></ol><p>By applying the above rules, we can derive below conditions</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">![] + [] === <span class="string">&quot;false&quot;</span></span><br><span class="line">+!+[] === <span class="number">1</span></span><br></pre></td></tr></table></figure><p>Now the character pattern would be created as below,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">      s               e               l               f</span><br><span class="line"> ^^^^^^^^^^^^^   ^^^^^^^^^^^^^   ^^^^^^^^^^^^^   ^^^^^^^^^^^^^</span><br><span class="line"></span><br><span class="line"> (![] + [])[<span class="number">3</span>] + (![] + [])[<span class="number">4</span>] + (![] + [])[<span class="number">2</span>] + (![] + [])[<span class="number">0</span>]</span><br><span class="line"> ^^^^^^^^^^^^^   ^^^^^^^^^^^^^   ^^^^^^^^^^^^^   ^^^^^^^^^^^^^</span><br><span class="line">(![] + [])[+!+[]+!+[]+!+[]] +</span><br><span class="line">(![] + [])[+!+[]+!+[]+!+[]+!+[]] +</span><br><span class="line">(![] + [])[+!+[]+!+[]] +</span><br><span class="line">(![] + [])[+[]]</span><br><span class="line">^^^^^^^^^^^^^^^^^^^^^^^^^^^^^</span><br><span class="line">(![]+[])[+!+[]+!+[]+!+[]]+(![]+[])[+!+[]+!+[]+!+[]+!+[]]+(![]+[])[+!+[]+!+[]]+(![]+[])[+[]]</span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="How-do-you-remove-falsy-values-from-an-array"><a href="#How-do-you-remove-falsy-values-from-an-array" class="headerlink" title="How do you remove falsy values from an array"></a>How do you remove falsy values from an array</h3><p>You can apply the filter method on the array by passing Boolean as a parameter. This way it removes all falsy values(0, undefined, null, false and “”) from the array.</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> myArray = [<span class="literal">false</span>, <span class="literal">null</span>, <span class="number">1</span>,<span class="number">5</span>, <span class="literal">undefined</span>]</span><br><span class="line">myArray.filter(<span class="built_in">Boolean</span>); <span class="comment">// [1, 5] // is same as myArray.filter(x =&gt; x);</span></span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="How-do-you-get-unique-values-of-an-array"><a href="#How-do-you-get-unique-values-of-an-array" class="headerlink" title="How do you get unique values of an array"></a>How do you get unique values of an array</h3><p>You can get unique values of an array with the combination of <code>Set</code> and rest expression/spread(…) syntax.</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">console</span>.log([...new <span class="built_in">Set</span>([<span class="number">1</span>, <span class="number">2</span>, <span class="number">4</span>, <span class="number">4</span>, <span class="number">3</span>])]); <span class="comment">// [1, 2, 4, 3]</span></span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-destructuring-aliases"><a href="#What-is-destructuring-aliases" class="headerlink" title="What is destructuring aliases"></a>What is destructuring aliases</h3><p>Sometimes you would like to have a destructured variable with a different name than the property name. In that case, you’ll use a <code>: newName</code> to specify a name for the variable. This process is called destructuring aliases.</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> obj = &#123; <span class="attr">x</span>: <span class="number">1</span> &#125;;</span><br><span class="line"><span class="comment">// Grabs obj.x as as &#123; otherName &#125;</span></span><br><span class="line"><span class="keyword">const</span> &#123; <span class="attr">x</span>: otherName &#125; = obj;</span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="How-do-you-map-the-array-values-without-using-map-method"><a href="#How-do-you-map-the-array-values-without-using-map-method" class="headerlink" title="How do you map the array values without using map method"></a>How do you map the array values without using map method</h3><p>You can map the array values without using the <code>map</code> method by just using the <code>from</code> method of Array. Let’s map city names from Countries array,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> countries = [</span><br><span class="line">    &#123; <span class="attr">name</span>: <span class="string">&#x27;India&#x27;</span>, <span class="attr">capital</span>: <span class="string">&#x27;Delhi&#x27;</span> &#125;,</span><br><span class="line">    &#123; <span class="attr">name</span>: <span class="string">&#x27;US&#x27;</span>, <span class="attr">capital</span>: <span class="string">&#x27;Washington&#x27;</span> &#125;,</span><br><span class="line">    &#123; <span class="attr">name</span>: <span class="string">&#x27;Russia&#x27;</span>, <span class="attr">capital</span>: <span class="string">&#x27;Moscow&#x27;</span> &#125;,</span><br><span class="line">    &#123; <span class="attr">name</span>: <span class="string">&#x27;Singapore&#x27;</span>, <span class="attr">capital</span>: <span class="string">&#x27;Singapore&#x27;</span> &#125;,</span><br><span class="line">    &#123; <span class="attr">name</span>: <span class="string">&#x27;China&#x27;</span>, <span class="attr">capital</span>: <span class="string">&#x27;Beijing&#x27;</span> &#125;,</span><br><span class="line">    &#123; <span class="attr">name</span>: <span class="string">&#x27;France&#x27;</span>, <span class="attr">capital</span>: <span class="string">&#x27;Paris&#x27;</span> &#125;,</span><br><span class="line">];</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> cityNames = <span class="built_in">Array</span>.from(countries, <span class="function">(<span class="params">&#123; capital&#125;</span>) =&gt;</span> capital);</span><br><span class="line"><span class="built_in">console</span>.log(cityNames); <span class="comment">// [&#x27;Delhi, &#x27;Washington&#x27;, &#x27;Moscow&#x27;, &#x27;Singapore&#x27;, &#x27;Beijing&#x27;, &#x27;Paris&#x27;]</span></span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="How-do-you-empty-an-array"><a href="#How-do-you-empty-an-array" class="headerlink" title="How do you empty an array"></a>How do you empty an array</h3><p>You can empty an array quickly by setting the array length to zero.</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> cities = [<span class="string">&#x27;Singapore&#x27;</span>, <span class="string">&#x27;Delhi&#x27;</span>, <span class="string">&#x27;London&#x27;</span>];</span><br><span class="line">cities.length = <span class="number">0</span>; <span class="comment">// cities becomes []</span></span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="How-do-you-rounding-numbers-to-certain-decimals"><a href="#How-do-you-rounding-numbers-to-certain-decimals" class="headerlink" title="How do you rounding numbers to certain decimals"></a>How do you rounding numbers to certain decimals</h3><p>You can round numbers to a certain number of decimals using <code>toFixed</code> method from native javascript.</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> pie = <span class="number">3.141592653</span>;</span><br><span class="line">pie = pie.toFixed(<span class="number">3</span>); <span class="comment">// 3.142</span></span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-the-easiest-way-to-convert-an-array-to-an-object"><a href="#What-is-the-easiest-way-to-convert-an-array-to-an-object" class="headerlink" title="What is the easiest way to convert an array to an object"></a>What is the easiest way to convert an array to an object</h3><p>You can convert an array to an object with the same data using spread(…) operator.</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> fruits = [<span class="string">&quot;banana&quot;</span>, <span class="string">&quot;apple&quot;</span>, <span class="string">&quot;orange&quot;</span>, <span class="string">&quot;watermelon&quot;</span>];</span><br><span class="line"><span class="keyword">var</span> fruitsObject = &#123;...fruits&#125;;</span><br><span class="line"><span class="built_in">console</span>.log(fruitsObject); <span class="comment">// &#123;0: &quot;banana&quot;, 1: &quot;apple&quot;, 2: &quot;orange&quot;, 3: &quot;watermelon&quot;&#125;</span></span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="How-do-you-create-an-array-with-some-data"><a href="#How-do-you-create-an-array-with-some-data" class="headerlink" title="How do you create an array with some data"></a>How do you create an array with some data</h3><p>You can create an array with some data or an array with the same values using <code>fill</code> method.</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> newArray = <span class="keyword">new</span> <span class="built_in">Array</span>(<span class="number">5</span>).fill(<span class="string">&quot;0&quot;</span>);</span><br><span class="line"><span class="built_in">console</span>.log(newArray); <span class="comment">// [&quot;0&quot;, &quot;0&quot;, &quot;0&quot;, &quot;0&quot;, &quot;0&quot;]</span></span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-are-the-placeholders-from-console-object"><a href="#What-are-the-placeholders-from-console-object" class="headerlink" title="What are the placeholders from console object"></a>What are the placeholders from console object</h3><p>Below are the list of placeholders available from console object,</p><ol><li>%o — It takes an object,</li><li>%s — It takes a string,</li><li>%d — It is used for a decimal or integer<br>These placeholders can be represented in the console.log as below</li></ol><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> user = &#123; <span class="string">&quot;name&quot;</span>:<span class="string">&quot;John&quot;</span>, <span class="string">&quot;id&quot;</span>: <span class="number">1</span>, <span class="string">&quot;city&quot;</span>: <span class="string">&quot;Delhi&quot;</span>&#125;;</span><br><span class="line"><span class="built_in">console</span>.log(<span class="string">&quot;Hello %s, your details %o are available in the object form&quot;</span>, <span class="string">&quot;John&quot;</span>, user); <span class="comment">// Hello John, your details &#123;name: &quot;John&quot;, id: 1, city: &quot;Delhi&quot;&#125; are available in object</span></span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="Is-it-possible-to-add-CSS-to-console-messages"><a href="#Is-it-possible-to-add-CSS-to-console-messages" class="headerlink" title="Is it possible to add CSS to console messages"></a>Is it possible to add CSS to console messages</h3><p>Yes, you can apply CSS styles to console messages similar to html text on the web page.</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">console</span>.log(<span class="string">&#x27;%c The text has blue color, with large font and red background&#x27;</span>, <span class="string">&#x27;color: blue; font-size: x-large; background: red&#x27;</span>);</span><br></pre></td></tr></table></figure><p>The text will be displayed as below,<br><img src="/images/console-css.png" alt="Screenshot"></p><p><strong>Note:</strong> All CSS styles can be applied to console messages.</p><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-the-purpose-of-dir-method-of-console-object"><a href="#What-is-the-purpose-of-dir-method-of-console-object" class="headerlink" title="What is the purpose of dir method of console object"></a>What is the purpose of dir method of console object</h3><p>The <code>console.dir()</code> is used to display an interactive list of the properties of the specified JavaScript object as JSON.</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> user = &#123; <span class="string">&quot;name&quot;</span>:<span class="string">&quot;John&quot;</span>, <span class="string">&quot;id&quot;</span>: <span class="number">1</span>, <span class="string">&quot;city&quot;</span>: <span class="string">&quot;Delhi&quot;</span>&#125;;</span><br><span class="line"><span class="built_in">console</span>.dir(user);</span><br></pre></td></tr></table></figure><p>The user object displayed in JSON representation<br><img src="/images/console-dir.png" alt="Screenshot"></p><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="Is-it-possible-to-debug-HTML-elements-in-console"><a href="#Is-it-possible-to-debug-HTML-elements-in-console" class="headerlink" title="Is it possible to debug HTML elements in console"></a>Is it possible to debug HTML elements in console</h3><p>Yes, it is possible to get and debug HTML elements in the console just like inspecting elements.</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> element = <span class="built_in">document</span>.getElementsByTagName(<span class="string">&quot;body&quot;</span>)[<span class="number">0</span>];</span><br><span class="line"><span class="built_in">console</span>.log(element);</span><br></pre></td></tr></table></figure><p>It prints the HTML element in the console,</p><p><img src="/images/console-html.png" alt="Screenshot"></p><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="How-do-you-display-data-in-a-tabular-format-using-console-object"><a href="#How-do-you-display-data-in-a-tabular-format-using-console-object" class="headerlink" title="How do you display data in a tabular format using console object"></a>How do you display data in a tabular format using console object</h3><p>The <code>console.table()</code> is used to display data in the console in a tabular format to visualize complex arrays or objects.</p><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> users = [&#123; <span class="string">&quot;name&quot;</span>:<span class="string">&quot;John&quot;</span>, <span class="string">&quot;id&quot;</span>: <span class="number">1</span>, <span class="string">&quot;city&quot;</span>: <span class="string">&quot;Delhi&quot;</span>&#125;, &#123; <span class="string">&quot;name&quot;</span>:<span class="string">&quot;Max&quot;</span>, <span class="string">&quot;id&quot;</span>: <span class="number">2</span>, <span class="string">&quot;city&quot;</span>: <span class="string">&quot;London&quot;</span>&#125;, &#123; <span class="string">&quot;name&quot;</span>:<span class="string">&quot;Rod&quot;</span>, <span class="string">&quot;id&quot;</span>: <span class="number">3</span>, <span class="string">&quot;city&quot;</span>: <span class="string">&quot;Paris&quot;</span>&#125; ];</span><br><span class="line"><span class="built_in">console</span>.table(users);</span><br></pre></td></tr></table></figure><p>The data visualized in a table format,</p><p><img src="/images/console-table.png" alt="Screenshot"><br><strong>Not:</strong> Remember that <code>console.table()</code> is not supported in IE.</p><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="How-do-you-verify-that-an-argument-is-a-Number-or-not"><a href="#How-do-you-verify-that-an-argument-is-a-Number-or-not" class="headerlink" title="How do you verify that an argument is a Number or not"></a>How do you verify that an argument is a Number or not</h3><p>The combination of IsNaN and isFinite methods are used to confirm whether an argument is a number or not.</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">isNumber</span>(<span class="params">n</span>)</span>&#123;</span><br><span class="line">    <span class="keyword">return</span> !<span class="built_in">isNaN</span>(<span class="built_in">parseFloat</span>(n)) &amp;&amp; <span class="built_in">isFinite</span>(n);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="How-do-you-create-copy-to-clipboard-button"><a href="#How-do-you-create-copy-to-clipboard-button" class="headerlink" title="How do you create copy to clipboard button"></a>How do you create copy to clipboard button</h3><p>You need to select the content(using .select() method) of the input element and execute the copy command with execCommand (i.e, execCommand(‘copy’)). You can also execute other system commands like cut and paste.</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">document</span>.querySelector(<span class="string">&quot;#copy-button&quot;</span>).onclick = <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">  <span class="comment">// Select the content</span></span><br><span class="line">  <span class="built_in">document</span>.querySelector(<span class="string">&quot;#copy-input&quot;</span>).select();</span><br><span class="line">  <span class="comment">// Copy to the clipboard</span></span><br><span class="line">  <span class="built_in">document</span>.execCommand(<span class="string">&#x27;copy&#x27;</span>);</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-the-shortcut-to-get-timestamp"><a href="#What-is-the-shortcut-to-get-timestamp" class="headerlink" title="What is the shortcut to get timestamp"></a>What is the shortcut to get timestamp</h3><p>You can use <code>new Date().getTime()</code> to get the current timestamp. There is an alternative shortcut to get the value.</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">console</span>.log(+<span class="keyword">new</span> <span class="built_in">Date</span>());</span><br><span class="line"><span class="built_in">console</span>.log(<span class="built_in">Date</span>.now());</span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="How-do-you-flattening-multi-dimensional-arrays"><a href="#How-do-you-flattening-multi-dimensional-arrays" class="headerlink" title="How do you flattening multi dimensional arrays"></a>How do you flattening multi dimensional arrays</h3><p>Flattening bi-dimensional arrays is trivial with Spread operator.</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> biDimensionalArr = [<span class="number">11</span>, [<span class="number">22</span>, <span class="number">33</span>], [<span class="number">44</span>, <span class="number">55</span>], [<span class="number">66</span>, <span class="number">77</span>], <span class="number">88</span>, <span class="number">99</span>];</span><br><span class="line"><span class="keyword">const</span> flattenArr = [].concat(...biDimensionalArr); <span class="comment">// [11, 22, 33, 44, 55, 66, 77, 88, 99]</span></span><br></pre></td></tr></table></figure><p>But you can make it work with multi-dimensional arrays by recursive calls,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">flattenMultiArray</span>(<span class="params">arr</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">const</span> flattened = [].concat(...arr);</span><br><span class="line">    <span class="keyword">return</span> flattened.some(<span class="function"><span class="params">item</span> =&gt;</span> <span class="built_in">Array</span>.isArray(item)) ? flattenMultiArray(flattened) : flattened;</span><br><span class="line"> &#125;</span><br><span class="line"><span class="keyword">const</span> multiDimensionalArr = [<span class="number">11</span>, [<span class="number">22</span>, <span class="number">33</span>], [<span class="number">44</span>, [<span class="number">55</span>, <span class="number">66</span>, [<span class="number">77</span>, [<span class="number">88</span>]], <span class="number">99</span>]]];</span><br><span class="line"><span class="keyword">const</span> flatArr = flattenMultiArray(multiDimensionalArr); <span class="comment">// [11, 22, 33, 44, 55, 66, 77, 88, 99]</span></span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-the-easiest-multi-condition-checking"><a href="#What-is-the-easiest-multi-condition-checking" class="headerlink" title="What is the easiest multi condition checking"></a>What is the easiest multi condition checking</h3><p>You can use <code>indexOf</code> to compare input with multiple values instead of checking each value as one condition.</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// Verbose approach</span></span><br><span class="line"><span class="keyword">if</span> (input === <span class="string">&#x27;first&#x27;</span> || input === <span class="number">1</span> || input === <span class="string">&#x27;second&#x27;</span> || input === <span class="number">2</span>) &#123;</span><br><span class="line">  someFunction();</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// Shortcut</span></span><br><span class="line"><span class="keyword">if</span> ([<span class="string">&#x27;first&#x27;</span>, <span class="number">1</span>, <span class="string">&#x27;second&#x27;</span>, <span class="number">2</span>].indexOf(input) !== -<span class="number">1</span>) &#123;</span><br><span class="line">  someFunction();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="How-do-you-capture-browser-back-button"><a href="#How-do-you-capture-browser-back-button" class="headerlink" title="How do you capture browser back button"></a>How do you capture browser back button</h3><p>The <code>window.onbeforeunload</code> method is used to capture browser back button events. This is helpful to warn users about losing the current data.</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">window</span>.onbeforeunload = <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">   alert(<span class="string">&quot;You work will be lost&quot;</span>);</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="How-do-you-disable-right-click-in-the-web-page"><a href="#How-do-you-disable-right-click-in-the-web-page" class="headerlink" title="How do you disable right click in the web page"></a>How do you disable right click in the web page</h3><p>The right click on the page can be disabled by returning false from the <code>oncontextmenu</code> attribute on the body element.</p><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">body</span> <span class="attr">oncontextmenu</span>=<span class="string">&quot;return false;&quot;</span>&gt;</span></span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-are-wrapper-objects"><a href="#What-are-wrapper-objects" class="headerlink" title="What are wrapper objects"></a>What are wrapper objects</h3><p>Primitive Values like string,number and boolean don’t have properties and methods but they are temporarily converted or coerced to an object(Wrapper object) when you try to perform actions on them. For example, if you apply toUpperCase() method on a primitive string value, it does not throw an error but returns uppercase of the string.</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> name = <span class="string">&quot;john&quot;</span>;</span><br><span class="line"></span><br><span class="line"><span class="built_in">console</span>.log(name.toUpperCase());  <span class="comment">// Behind the scenes treated as console.log(new String(name).toUpperCase());</span></span><br></pre></td></tr></table></figure><p>i.e, Every primitive except null and undefined have Wrapper Objects and the list of wrapper objects are String,Number,Boolean,Symbol and BigInt.</p><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-AJAX"><a href="#What-is-AJAX" class="headerlink" title="What is AJAX"></a>What is AJAX</h3><p>AJAX stands for Asynchronous JavaScript and XML and it is a group of related technologies(HTML, CSS, JavaScript, XMLHttpRequest API etc) used to display data asynchronously. i.e. We can send data to the server and get data from the server without reloading the web page.</p><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-are-the-different-ways-to-deal-with-Asynchronous-Code"><a href="#What-are-the-different-ways-to-deal-with-Asynchronous-Code" class="headerlink" title="What are the different ways to deal with Asynchronous Code"></a>What are the different ways to deal with Asynchronous Code</h3><p>Below are the list of different ways to deal with Asynchronous code.</p><ol><li>Callbacks</li><li>Promises</li><li>Async/await</li><li>Third-party libraries such as async.js,bluebird etc</li></ol><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="How-to-cancel-a-fetch-request"><a href="#How-to-cancel-a-fetch-request" class="headerlink" title="How to cancel a fetch request"></a>How to cancel a fetch request</h3><p>Until a few days back, One shortcoming of native promises is no direct way to cancel a fetch request. But the new <code>AbortController</code> from js specification allows you to use a signal to abort one or multiple fetch calls.<br>The basic flow of cancelling a fetch request would be as below,</p><ol><li>Create an <code>AbortController</code> instance</li><li>Get the signal property of an instance and pass the signal as a fetch option for signal</li><li>Call the AbortController’s abort property to cancel all fetches that use that signal<br>For example, let’s pass the same signal to multiple fetch calls will cancel all requests with that signal,</li></ol><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> controller = <span class="keyword">new</span> AbortController();</span><br><span class="line"><span class="keyword">const</span> &#123; signal &#125; = controller;</span><br><span class="line"></span><br><span class="line">fetch(<span class="string">&quot;http://localhost:8000&quot;</span>, &#123; signal &#125;).then(<span class="function"><span class="params">response</span> =&gt;</span> &#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">`Request 1 is complete!`</span>);</span><br><span class="line">&#125;).catch(<span class="function"><span class="params">e</span> =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">if</span>(e.name === <span class="string">&quot;AbortError&quot;</span>) &#123;</span><br><span class="line">        <span class="comment">// We know it&#x27;s been canceled!</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;);</span><br><span class="line"></span><br><span class="line">fetch(<span class="string">&quot;http://localhost:8000&quot;</span>, &#123; signal &#125;).then(<span class="function"><span class="params">response</span> =&gt;</span> &#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">`Request 2 is complete!`</span>);</span><br><span class="line">&#125;).catch(<span class="function"><span class="params">e</span> =&gt;</span> &#123;</span><br><span class="line">     <span class="keyword">if</span>(e.name === <span class="string">&quot;AbortError&quot;</span>) &#123;</span><br><span class="line">         <span class="comment">// We know it&#x27;s been canceled!</span></span><br><span class="line">      &#125;</span><br><span class="line">&#125;);</span><br><span class="line"></span><br><span class="line"><span class="comment">// Wait 2 seconds to abort both requests</span></span><br><span class="line"><span class="built_in">setTimeout</span>(<span class="function">() =&gt;</span> controller.abort(), <span class="number">2000</span>);</span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-web-speech-API"><a href="#What-is-web-speech-API" class="headerlink" title="What is web speech API"></a>What is web speech API</h3><p>Web speech API is used to enable modern browsers recognize and synthesize speech(i.e, voice data into web apps). This API has been introduced by W3C Community in the year 2012. It has two main parts,</p><ol><li><strong>SpeechRecognition (Asynchronous Speech Recognition or Speech-to-Text):</strong> It provides the ability to recognize voice context from an audio input and respond accordingly. This is accessed by the <code>SpeechRecognition</code> interface.<br>The below example shows on how to use this API to get text from speech,</li></ol><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">window</span>.SpeechRecognition = <span class="built_in">window</span>.webkitSpeechRecognition || <span class="built_in">window</span>.SpeechRecognition;  <span class="comment">// webkitSpeechRecognition for Chrome and SpeechRecognition for FF</span></span><br><span class="line"><span class="keyword">const</span> recognition = <span class="keyword">new</span> <span class="built_in">window</span>.SpeechRecognition();</span><br><span class="line">recognition.onresult = <span class="function">(<span class="params">event</span>) =&gt;</span> &#123; <span class="comment">// SpeechRecognitionEvent type</span></span><br><span class="line">  <span class="keyword">const</span> speechToText = event.results[<span class="number">0</span>][<span class="number">0</span>].transcript;</span><br><span class="line">  <span class="built_in">console</span>.log(speechToText);</span><br><span class="line">&#125;</span><br><span class="line">recognition.start();</span><br></pre></td></tr></table></figure><p>In this API, browser is going to ask you for permission to use your microphone</p><ol><li><strong>SpeechSynthesis (Text-to-Speech):</strong> It provides the ability to recognize voice context from an audio input and respond. This is accessed by the <code>SpeechSynthesis</code> interface.<br>For example, the below code is used to get voice/speech from text,</li></ol><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">if</span>(<span class="string">&#x27;speechSynthesis&#x27;</span> <span class="keyword">in</span> <span class="built_in">window</span>)&#123;</span><br><span class="line">    <span class="keyword">var</span> speech = <span class="keyword">new</span> SpeechSynthesisUtterance(<span class="string">&#x27;Hello World!&#x27;</span>);</span><br><span class="line">    speech.lang = <span class="string">&#x27;en-US&#x27;</span>;</span><br><span class="line">    <span class="built_in">window</span>.speechSynthesis.speak(speech);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>The above examples can be tested on chrome(33+) browser’s developer console.<br><strong>Note:</strong> This API is still a working draft and only available in Chrome and Firefox browsers(ofcourse Chrome only implemented the specification)<br><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-minimum-timeout-throttling"><a href="#What-is-minimum-timeout-throttling" class="headerlink" title="What is minimum timeout throttling"></a>What is minimum timeout throttling</h3><p>Both browser and NodeJS javascript environments throttles with a minimum delay that is greater than 0ms. That means even though setting a delay of 0ms will not happen instantaneously.<br><strong>Browsers:</strong> They have a minimum delay of 4ms. This throttle occurs when successive calls are triggered due to callback nesting(certain depth) or after a certain number of successive intervals.<br>Note: The older browsers have a minimum delay of 10ms.<br><strong>Nodejs:</strong> They have a minimum delay of 1ms. This throttle happens when the delay is larger than 2147483647 or less than 1.<br>The best example to explain this timeout throttling behavior is the order of below code snippet.</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">runMeFirst</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">&#x27;My script is initialized&#x27;</span>);</span><br><span class="line">&#125;</span><br><span class="line"><span class="built_in">setTimeout</span>(runMeFirst, <span class="number">0</span>);</span><br><span class="line"><span class="built_in">console</span>.log(<span class="string">&#x27;Script loaded&#x27;</span>);</span><br></pre></td></tr></table></figure><p>and the output would be in</p><figure class="highlight cmd"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">Script loaded</span><br><span class="line">My script is initialized</span><br></pre></td></tr></table></figure><p>If you don’t use <code>setTimeout</code>, the order of logs will be sequential.</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">runMeFirst</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">   <span class="built_in">console</span>.log(<span class="string">&#x27;My script is initialized&#x27;</span>);</span><br><span class="line">&#125;</span><br><span class="line">runMeFirst();</span><br><span class="line"><span class="built_in">console</span>.log(<span class="string">&#x27;Script loaded&#x27;</span>);</span><br></pre></td></tr></table></figure><p>and the output is,</p><figure class="highlight cmd"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">My script is initialized</span><br><span class="line">Script loaded</span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="How-do-you-implement-zero-timeout-in-modern-browsers"><a href="#How-do-you-implement-zero-timeout-in-modern-browsers" class="headerlink" title="How do you implement zero timeout in modern browsers"></a>How do you implement zero timeout in modern browsers</h3><p>You can’t use setTimeout(fn, 0) to execute the code immediately due to minimum delay of greater than 0ms. But you can use window.postMessage() to achieve this behavior.</p><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-are-tasks-in-event-loop"><a href="#What-are-tasks-in-event-loop" class="headerlink" title="What are tasks in event loop"></a>What are tasks in event loop</h3><p>A task is any javascript code/program which is scheduled to be run by the standard mechanisms such as initially starting to run a program, run an event callback, or an interval or timeout being fired. All these tasks are scheduled on a task queue.<br>Below are the list of use cases to add tasks to the task queue,</p><ol><li>When a new javascript program is executed directly from console or running by the <code>&lt;script&gt;</code> element, the task will be added to the task queue.</li><li>When an event fires, the event callback added to task queue</li><li>When a setTimeout or setInterval is reached, the corresponding callback added to task queue</li></ol><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-microtask"><a href="#What-is-microtask" class="headerlink" title="What is microtask"></a>What is microtask</h3><p>Microtask is the javascript code which needs to be executed immediately after the currently executing task/microtask is completed. They are kind of blocking in nature. i.e, The main thread will be blocked until the microtask queue is empty.<br>The main sources of microtasks are Promise.resolve, Promise.reject, MutationObservers, IntersectionObservers etc</p><p><strong>Note:</strong> All of these microtasks are processed in the same turn of the event loop.<br><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-are-different-event-loops"><a href="#What-are-different-event-loops" class="headerlink" title="What are different event loops"></a>What are different event loops</h3><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-the-purpose-of-queueMicrotask"><a href="#What-is-the-purpose-of-queueMicrotask" class="headerlink" title="What is the purpose of queueMicrotask"></a>What is the purpose of queueMicrotask</h3><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="How-do-you-use-javascript-libraries-in-typescript-file"><a href="#How-do-you-use-javascript-libraries-in-typescript-file" class="headerlink" title="How do you use javascript libraries in typescript file"></a>How do you use javascript libraries in typescript file</h3><p>It is known that not all JavaScript libraries or frameworks have TypeScript declaration files. But if you still want to use libraries or frameworks in our TypeScript files without getting compilation errors, the only solution is <code>declare</code> keyword along with a variable declaration. For example, let’s imagine you have a library called <code>customLibrary</code> that doesn’t have a TypeScript declaration and have a namespace called <code>customLibrary</code> in the global namespace. You can use this library in typescript code as below,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">declare <span class="keyword">var</span> customLibrary;</span><br></pre></td></tr></table></figure><p>In the runtime, typescript will provide the type to the <code>customLibrary</code> variable as <code>any</code> type. The another alternative without using declare keyword is below</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> customLibrary: any;</span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-are-the-differences-between-promises-and-observables"><a href="#What-are-the-differences-between-promises-and-observables" class="headerlink" title="What are the differences between promises and observables"></a>What are the differences between promises and observables</h3><p>Some of the major difference in a tabular form</p><table><thead><tr><th>Promises</th><th>Observables</th></tr></thead><tbody><tr><td>Emits only a single value at a time</td><td>Emits multiple values over a period of time(stream of values ranging from 0 to multiple)</td></tr><tr><td>Eager in nature; they are going to be called immediately</td><td>Lazy in nature; they require subscription to be invoked</td></tr><tr><td>Promise is always asynchronous even though it resolved immediately</td><td>Observable can be either synchronous or asynchronous</td></tr><tr><td>Doesn’t provide any operators</td><td>Provides operators such as map, forEach, filter, reduce, retry, and retryWhen etc</td></tr><tr><td>Cannot be canceled</td><td>Canceled by using unsubscribe() method</td></tr></tbody></table><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-heap"><a href="#What-is-heap" class="headerlink" title="What is heap"></a>What is heap</h3><p>Heap(Or memory heap) is the memory location where objects are stored when we define variables. i.e, This is the place where all the memory allocations and de-allocation take place. Both heap and call-stack are two containers of JS runtime.<br>Whenever runtime comes across variables and function declarations in the code it stores them in the Heap.</p><p><img src="/images/heap.png" alt="Screenshot"></p><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-an-event-table"><a href="#What-is-an-event-table" class="headerlink" title="What is an event table"></a>What is an event table</h3><p>Event Table is a data structure that stores and keeps track of all the events which will be executed asynchronously like after some time interval or after the resolution of some API requests. i.e Whenever you call a setTimeout function or invoke async operation, it is added to the Event Table.<br>It doesn’t not execute functions on it’s own. The main purpose of the event table is to keep track of events and send them to the Event Queue as shown in the below diagram.</p><p><img src="/images/event-table.png" alt="Screenshot"></p><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-a-microTask-queue"><a href="#What-is-a-microTask-queue" class="headerlink" title="What is a microTask queue"></a>What is a microTask queue</h3><p>Microtask Queue is the new queue where all the tasks initiated by promise objects get processed before the callback queue.<br>The microtasks queue are processed before the next rendering and painting jobs. But if these microtasks are running for a long time then it leads to visual degradation.</p><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-the-difference-between-shim-and-polyfill"><a href="#What-is-the-difference-between-shim-and-polyfill" class="headerlink" title="What is the difference between shim and polyfill"></a>What is the difference between shim and polyfill</h3><p>A shim is a library that brings a new API to an older environment, using only the means of that environment. It isn’t necessarily restricted to a web application. For example, es5-shim.js is used to emulate ES5 features on older browsers (mainly pre IE9).<br>Whereas polyfill is a piece of code (or plugin) that provides the technology that you, the developer, expect the browser to provide natively.<br>In a simple sentence, A polyfill is a shim for a browser API.</p><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="How-do-you-detect-primitive-or-non-primitive-value-type"><a href="#How-do-you-detect-primitive-or-non-primitive-value-type" class="headerlink" title="How do you detect primitive or non primitive value type"></a>How do you detect primitive or non primitive value type</h3><p>In JavaScript, primitive types include boolean, string, number, BigInt, null, Symbol and undefined. Whereas non-primitive types include the Objects. But you can easily identify them with the below function,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> myPrimitive = <span class="number">30</span>;</span><br><span class="line"><span class="keyword">var</span> myNonPrimitive = &#123;&#125;;</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">isPrimitive</span>(<span class="params">val</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="built_in">Object</span>(val) !== val;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">isPrimitive(myPrimitive);</span><br><span class="line">isPrimitive(myNonPrimitive);</span><br></pre></td></tr></table></figure><p>If the value is a primitive data type, the Object constructor creates a new wrapper object for the value. But If the value is a non-primitive data type (an object), the Object constructor will give the same object.</p><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-babel"><a href="#What-is-babel" class="headerlink" title="What is babel"></a>What is babel</h3><p>Babel is a JavaScript transpiler to convert ECMAScript 2015+ code into a backwards compatible version of JavaScript in current and older browsers or environments. Some of the main features are listed below,</p><ol><li>Transform syntax</li><li>Polyfill features that are missing in your target environment (using @babel/polyfill)</li><li>Source code transformations (or codemods)</li></ol><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="Is-Node-js-completely-single-threaded"><a href="#Is-Node-js-completely-single-threaded" class="headerlink" title="Is Node.js completely single threaded"></a>Is Node.js completely single threaded</h3><p>Node is a single thread, but some of the functions included in the Node.js standard library(e.g, fs module functions) are not single threaded. i.e, Their logic runs outside of the Node.js single thread to improve the speed and performance of a program.</p><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-are-the-common-use-cases-of-observables"><a href="#What-are-the-common-use-cases-of-observables" class="headerlink" title="What are the common use cases of observables"></a>What are the common use cases of observables</h3><p>Some of the most common use cases of observables are web sockets with push notifications, user input changes, repeating intervals, etc</p><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-RxJS"><a href="#What-is-RxJS" class="headerlink" title="What is RxJS"></a>What is RxJS</h3><p>RxJS (Reactive Extensions for JavaScript) is a library for implementing reactive programming using observables that makes it easier to compose asynchronous or callback-based code. It also provides utility functions for creating and working with observables.</p><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-the-difference-between-Function-constructor-and-function-declaration"><a href="#What-is-the-difference-between-Function-constructor-and-function-declaration" class="headerlink" title="What is the difference between Function constructor and function declaration"></a>What is the difference between Function constructor and function declaration</h3><p>The functions which are created with <code>Function constructor</code> do not create closures to their creation contexts but they are always created in the global scope. i.e, the function can access its own local variables and global scope variables only. Whereas function declarations can access outer function variables(closures) too.</p><p>Let’s see this difference with an example,</p><p><strong>Function Constructor:</strong></p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> a = <span class="number">100</span>;</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">createFunction</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="keyword">var</span> a = <span class="number">200</span>;</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">new</span> <span class="built_in">Function</span>(<span class="string">&#x27;return a;&#x27;</span>);</span><br><span class="line">&#125;</span><br><span class="line"><span class="built_in">console</span>.log(createFunction()()); <span class="comment">// 100</span></span><br></pre></td></tr></table></figure><p><strong>Function declaration:</strong></p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> a = <span class="number">100</span>;</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">createFunction</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="keyword">var</span> a = <span class="number">200</span>;</span><br><span class="line">    <span class="keyword">return</span> <span class="function"><span class="keyword">function</span> <span class="title">func</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> a;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="built_in">console</span>.log(createFunction()()); <span class="comment">// 200</span></span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-a-Short-circuit-condition"><a href="#What-is-a-Short-circuit-condition" class="headerlink" title="What is a Short circuit condition"></a>What is a Short circuit condition</h3><p>Short circuit conditions are meant for condensed way of writing simple if statements. Let’s demonstrate the scenario using an example. If you would like to login to a portal with an authentication condition, the expression would be as below,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">if</span> (authenticate) &#123;</span><br><span class="line">   loginToPorta();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>Since the javascript logical operators evaluated from left to right, the above expression can be simplified using &amp;&amp; logical operator</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">authenticate &amp;&amp; loginToPorta();</span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-the-easiest-way-to-resize-an-array"><a href="#What-is-the-easiest-way-to-resize-an-array" class="headerlink" title="What is the easiest way to resize an array"></a>What is the easiest way to resize an array</h3><p>The length property of an array is useful to resize or empty an array quickly. Let’s apply length property on number array to resize the number of elements from 5 to 2,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> array = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>];</span><br><span class="line"><span class="built_in">console</span>.log(array.length); <span class="comment">// 5</span></span><br><span class="line"></span><br><span class="line">array.length = <span class="number">2</span>;</span><br><span class="line"><span class="built_in">console</span>.log(array.length); <span class="comment">// 2</span></span><br><span class="line"><span class="built_in">console</span>.log(array); <span class="comment">// [1,2]</span></span><br></pre></td></tr></table></figure><p>and the array can be emptied too</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> array = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>];</span><br><span class="line">array.length = <span class="number">0</span>;</span><br><span class="line"><span class="built_in">console</span>.log(array.length); <span class="comment">// 0</span></span><br><span class="line"><span class="built_in">console</span>.log(array); <span class="comment">// []</span></span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-an-observable"><a href="#What-is-an-observable" class="headerlink" title="What is an observable"></a>What is an observable</h3><p>An Observable is basically a function that can return a stream of values either synchronously or asynchronously to an observer over time. The consumer can get the value by calling <code>subscribe()</code> method.<br>Let’s look at a simple example of an Observable</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> &#123; Observable &#125; <span class="keyword">from</span> <span class="string">&#x27;rxjs&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> observable = <span class="keyword">new</span> Observable(<span class="function"><span class="params">observer</span> =&gt;</span> &#123;</span><br><span class="line">  <span class="built_in">setTimeout</span>(<span class="function">() =&gt;</span> &#123;</span><br><span class="line">    observer.next(<span class="string">&#x27;Message from a Observable!&#x27;</span>);</span><br><span class="line">  &#125;, <span class="number">3000</span>);</span><br><span class="line">&#125;);</span><br><span class="line"></span><br><span class="line">observable.subscribe(<span class="function"><span class="params">value</span> =&gt;</span> <span class="built_in">console</span>.log(value));</span><br></pre></td></tr></table></figure><p><img src="/images/observables.png" alt="Screenshot"></p><p><strong>Note:</strong> Observables are not part of the JavaScript language yet but they are being proposed to be added to the language</p><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-the-difference-between-function-and-class-declarations"><a href="#What-is-the-difference-between-function-and-class-declarations" class="headerlink" title="What is the difference between function and class declarations"></a>What is the difference between function and class declarations</h3><p>The main difference between function declarations and class declarations is <code>hoisting</code>. The function declarations are hoisted but not class declarations.</p><p><strong>Classes:</strong></p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> user = <span class="keyword">new</span> User(); <span class="comment">// ReferenceError</span></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">User</span> </span>&#123;&#125;</span><br></pre></td></tr></table></figure><p><strong>Constructor Function:</strong></p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> user = <span class="keyword">new</span> User(); <span class="comment">// No error</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">User</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-an-async-function"><a href="#What-is-an-async-function" class="headerlink" title="What is an async function"></a>What is an async function</h3><p>An async function is a function declared with the <code>async</code> keyword which enables asynchronous, promise-based behavior to be written in a cleaner style by avoiding promise chains. These functions can contain zero or more <code>await</code> expressions.</p><p>Let’s take a below async function example,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">async</span> <span class="function"><span class="keyword">function</span> <span class="title">logger</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">let</span> data = <span class="keyword">await</span> fetch(<span class="string">&#x27;http://someapi.com/users&#x27;</span>); <span class="comment">// pause until fetch returns</span></span><br><span class="line">  <span class="built_in">console</span>.log(data)</span><br><span class="line">&#125;</span><br><span class="line">logger();</span><br></pre></td></tr></table></figure><p>It is basically syntax sugar over ES2015 promises and generators.</p><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="How-do-you-prevent-promises-swallowing-errors"><a href="#How-do-you-prevent-promises-swallowing-errors" class="headerlink" title="How do you prevent promises swallowing errors"></a>How do you prevent promises swallowing errors</h3><p>While using asynchronous code, JavaScript’s ES6 promises can make your life a lot easier without having callback pyramids and error handling on every second line. But Promises have some pitfalls and the biggest one is swallowing errors by default.</p><p>Let’s say you expect to print an error to the console for all the below cases,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">Promise</span>.resolve(<span class="string">&#x27;promised value&#x27;</span>).then(<span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">      <span class="keyword">throw</span> <span class="keyword">new</span> <span class="built_in">Error</span>(<span class="string">&#x27;error&#x27;</span>);</span><br><span class="line">&#125;);</span><br><span class="line"></span><br><span class="line"><span class="built_in">Promise</span>.reject(<span class="string">&#x27;error value&#x27;</span>).catch(<span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">      <span class="keyword">throw</span> <span class="keyword">new</span> <span class="built_in">Error</span>(<span class="string">&#x27;error&#x27;</span>);</span><br><span class="line">&#125;);</span><br><span class="line"></span><br><span class="line"><span class="keyword">new</span> <span class="built_in">Promise</span>(<span class="function"><span class="keyword">function</span>(<span class="params">resolve, reject</span>) </span>&#123;</span><br><span class="line">      <span class="keyword">throw</span> <span class="keyword">new</span> <span class="built_in">Error</span>(<span class="string">&#x27;error&#x27;</span>);</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure><p>But there are many modern JavaScript environments that won’t print any errors. You can fix this problem in different ways,</p><ol><li><p><strong>Add catch block at the end of each chain:</strong> You can add catch block to the end of each of your promise chains</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">Promise</span>.resolve(<span class="string">&#x27;promised value&#x27;</span>).then(<span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="keyword">throw</span> <span class="keyword">new</span> <span class="built_in">Error</span>(<span class="string">&#x27;error&#x27;</span>);</span><br><span class="line">&#125;).catch(<span class="function"><span class="keyword">function</span>(<span class="params">error</span>) </span>&#123;</span><br><span class="line">  <span class="built_in">console</span>.error(error.stack);</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure><p>But it is quite difficult to type for each promise chain and verbose too.</p></li><li><p><strong>Add done method:</strong> You can replace first solution’s then and catch blocks with done method</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">Promise</span>.resolve(<span class="string">&#x27;promised value&#x27;</span>).done(<span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="keyword">throw</span> <span class="keyword">new</span> <span class="built_in">Error</span>(<span class="string">&#x27;error&#x27;</span>);</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure><p>Let’s say you want to fetch data using HTTP and later perform processing on the resulting data asynchronously. You can write <code>done</code> block as below,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">getDataFromHttp()</span><br><span class="line">  .then(<span class="function"><span class="keyword">function</span>(<span class="params">result</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> processDataAsync(result);</span><br><span class="line">  &#125;)</span><br><span class="line">  .done(<span class="function"><span class="keyword">function</span>(<span class="params">processed</span>) </span>&#123;</span><br><span class="line">    displayData(processed);</span><br><span class="line">  &#125;);</span><br></pre></td></tr></table></figure><p>In future, if the processing library API changed to synchronous then you can remove <code>done</code> block as below,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">getDataFromHttp()</span><br><span class="line">  .then(<span class="function"><span class="keyword">function</span>(<span class="params">result</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> displayData(processDataAsync(result));</span><br><span class="line">  &#125;)</span><br></pre></td></tr></table></figure><p>and then you forgot to add <code>done</code> block to <code>then</code> block leads to silent errors.</p></li><li><p><strong>Extend ES6 Promises by Bluebird:</strong><br>Bluebird extends the ES6 Promises API to avoid the issue in the second solution. This library has a “default” onRejection handler which will print all errors from rejected Promises to stderr. After installation, you can process unhandled rejections</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">Promise</span>.onPossiblyUnhandledRejection(<span class="function"><span class="keyword">function</span>(<span class="params">error</span>)</span>&#123;</span><br><span class="line">    <span class="keyword">throw</span> error;</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure><p>and discard a rejection, just handle it with an empty catch</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">Promise</span>.reject(<span class="string">&#x27;error value&#x27;</span>).catch(<span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;&#125;);</span><br></pre></td></tr></table></figure></li></ol><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-deno"><a href="#What-is-deno" class="headerlink" title="What is deno"></a>What is deno</h3><p>Deno is a simple, modern and secure runtime for JavaScript and TypeScript that uses V8 JavaScript engine and the Rust programming language.</p><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="How-do-you-make-an-object-iterable-in-javascript"><a href="#How-do-you-make-an-object-iterable-in-javascript" class="headerlink" title="How do you make an object iterable in javascript"></a>How do you make an object iterable in javascript</h3><p>By default, plain objects are not iterable. But you can make the object iterable by defining a <code>Symbol.iterator</code> property on it.</p><p>Let’s demonstrate this with an example,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> collection = &#123;</span><br><span class="line">  one: <span class="number">1</span>,</span><br><span class="line">  two: <span class="number">2</span>,</span><br><span class="line">  three: <span class="number">3</span>,</span><br><span class="line">  [<span class="built_in">Symbol</span>.iterator]() &#123;</span><br><span class="line">    <span class="keyword">const</span> values = <span class="built_in">Object</span>.keys(<span class="built_in">this</span>);</span><br><span class="line">    <span class="keyword">let</span> i = <span class="number">0</span>;</span><br><span class="line">    <span class="keyword">return</span> &#123;</span><br><span class="line">      next: <span class="function">() =&gt;</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> &#123;</span><br><span class="line">          value: <span class="built_in">this</span>[values[i++]],</span><br><span class="line">          done: i &gt; values.length</span><br><span class="line">        &#125;</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> iterator = collection[<span class="built_in">Symbol</span>.iterator]();</span><br><span class="line"></span><br><span class="line"><span class="built_in">console</span>.log(iterator.next());    <span class="comment">// → &#123;value: 1, done: false&#125;</span></span><br><span class="line"><span class="built_in">console</span>.log(iterator.next());    <span class="comment">// → &#123;value: 2, done: false&#125;</span></span><br><span class="line"><span class="built_in">console</span>.log(iterator.next());    <span class="comment">// → &#123;value: 3, done: false&#125;</span></span><br><span class="line"><span class="built_in">console</span>.log(iterator.next());    <span class="comment">// → &#123;value: undefined, done: true&#125;</span></span><br></pre></td></tr></table></figure><p>The above process can be simplified using a generator function,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> collection = &#123;</span><br><span class="line">  one: <span class="number">1</span>,</span><br><span class="line">  two: <span class="number">2</span>,</span><br><span class="line">  three: <span class="number">3</span>,</span><br><span class="line">  [<span class="built_in">Symbol</span>.iterator]: <span class="function"><span class="keyword">function</span> * (<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">let</span> key <span class="keyword">in</span> <span class="built_in">this</span>) &#123;</span><br><span class="line">      <span class="keyword">yield</span> <span class="built_in">this</span>[key];</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;;</span><br><span class="line"><span class="keyword">const</span> iterator = collection[<span class="built_in">Symbol</span>.iterator]();</span><br><span class="line"><span class="built_in">console</span>.log(iterator.next());    <span class="comment">// &#123;value: 1, done: false&#125;</span></span><br><span class="line"><span class="built_in">console</span>.log(iterator.next());    <span class="comment">// &#123;value: 2, done: false&#125;</span></span><br><span class="line"><span class="built_in">console</span>.log(iterator.next());    <span class="comment">// &#123;value: 3, done: false&#125;</span></span><br><span class="line"><span class="built_in">console</span>.log(iterator.next());    <span class="comment">// &#123;value: undefined, done: true&#125;</span></span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-a-Proper-Tail-Call"><a href="#What-is-a-Proper-Tail-Call" class="headerlink" title="What is a Proper Tail Call"></a>What is a Proper Tail Call</h3><p>First, we should know about tail call before talking about “Proper Tail Call”. A tail call is a subroutine or function call performed as the final action of a calling function. Whereas <strong>Proper tail call(PTC)</strong> is a technique where the program or code will not create additional stack frames for a recursion when the function call is a tail call.</p><p>For example, the below classic or head recursion of factorial function relies on stack for each step. Each step need to be processed upto <code>n * factorial(n - 1)</code></p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">factorial</span>(<span class="params">n</span>) </span>&#123;</span><br><span class="line">  <span class="keyword">if</span> (n === <span class="number">0</span>) &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="number">1</span></span><br><span class="line">  &#125;</span><br><span class="line">  <span class="keyword">return</span> n * factorial(n - <span class="number">1</span>)</span><br><span class="line">&#125;</span><br><span class="line"><span class="built_in">console</span>.log(factorial(<span class="number">5</span>)); <span class="comment">//120</span></span><br></pre></td></tr></table></figure><p>But if you use Tail recursion functions, they keep passing all the necessary data it needs down the recursion without relying on the stack.</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">factorial</span>(<span class="params">n, acc = <span class="number">1</span></span>) </span>&#123;</span><br><span class="line">  <span class="keyword">if</span> (n === <span class="number">0</span>) &#123;</span><br><span class="line">    <span class="keyword">return</span> acc</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="keyword">return</span> factorial(n - <span class="number">1</span>, n * acc)</span><br><span class="line">&#125;</span><br><span class="line"><span class="built_in">console</span>.log(factorial(<span class="number">5</span>)); <span class="comment">//120</span></span><br></pre></td></tr></table></figure><p>The above pattern returns the same output as the first one. But the accumulator keeps track of total as an argument without using stack memory on recursive calls.</p><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="How-do-you-check-an-object-is-a-promise-or-not"><a href="#How-do-you-check-an-object-is-a-promise-or-not" class="headerlink" title="How do you check an object is a promise or not"></a>How do you check an object is a promise or not</h3><p>If you don’t know if a value is a promise or not, wrapping the value as <code>Promise.resolve(value)</code> which returns a promise</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">isPromise</span>(<span class="params">object</span>)</span>&#123;</span><br><span class="line">   <span class="keyword">if</span>(<span class="built_in">Promise</span> &amp;&amp; <span class="built_in">Promise</span>.resolve)&#123;</span><br><span class="line">   <span class="keyword">return</span> <span class="built_in">Promise</span>.resolve(object) == object;</span><br><span class="line">   &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">   <span class="keyword">throw</span> <span class="string">&quot;Promise not supported in your environment&quot;</span></span><br><span class="line">   &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> i = <span class="number">1</span>;</span><br><span class="line"><span class="keyword">var</span> promise = <span class="keyword">new</span> <span class="built_in">Promise</span>(<span class="function"><span class="keyword">function</span>(<span class="params">resolve,reject</span>)</span>&#123;</span><br><span class="line">   resolve()</span><br><span class="line">&#125;);</span><br><span class="line"></span><br><span class="line"><span class="built_in">console</span>.log(isPromise(i)); <span class="comment">// false</span></span><br><span class="line"><span class="built_in">console</span>.log(isPromise(p)); <span class="comment">// true</span></span><br></pre></td></tr></table></figure><p>Another way is to check for <code>.then()</code> handler type</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">isPromise</span>(<span class="params">value</span>) </span>&#123;</span><br><span class="line">   <span class="keyword">return</span> <span class="built_in">Boolean</span>(value &amp;&amp; <span class="keyword">typeof</span> value.then === <span class="string">&#x27;function&#x27;</span>);</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">var</span> i = <span class="number">1</span>;</span><br><span class="line"><span class="keyword">var</span> promise = <span class="keyword">new</span> <span class="built_in">Promise</span>(<span class="function"><span class="keyword">function</span>(<span class="params">resolve,reject</span>)</span>&#123;</span><br><span class="line">   resolve()</span><br><span class="line">&#125;);</span><br><span class="line"></span><br><span class="line"><span class="built_in">console</span>.log(isPromise(i)) <span class="comment">// false</span></span><br><span class="line"><span class="built_in">console</span>.log(isPromise(promise)); <span class="comment">// true</span></span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="How-to-detect-if-a-function-is-called-as-constructor"><a href="#How-to-detect-if-a-function-is-called-as-constructor" class="headerlink" title="How to detect if a function is called as constructor"></a>How to detect if a function is called as constructor</h3><p>You can use <code>new.target</code> pseudo-property to detect whether a function was called as a constructor(using the new operator) or as a regular function call.</p><ol><li><p>If a constructor or function invoked using the new operator, new.target returns a reference to the constructor or function.</p></li><li><p>For function calls, new.target is undefined.</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">Myfunc</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">   <span class="keyword">if</span> (<span class="keyword">new</span>.target) &#123;</span><br><span class="line">      <span class="built_in">console</span>.log(<span class="string">&#x27;called with new&#x27;</span>);</span><br><span class="line">   &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">      <span class="built_in">console</span>.log(<span class="string">&#x27;not called with new&#x27;</span>);</span><br><span class="line">   &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">new</span> Myfunc(); <span class="comment">// called with new</span></span><br><span class="line">Myfunc(); <span class="comment">// not called with new</span></span><br><span class="line">Myfunc.call(&#123;&#125;); not called <span class="keyword">with</span> <span class="keyword">new</span></span><br></pre></td></tr></table></figure></li></ol><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-are-the-differences-between-arguments-object-and-rest-parameter"><a href="#What-are-the-differences-between-arguments-object-and-rest-parameter" class="headerlink" title="What are the differences between arguments object and rest parameter"></a>What are the differences between arguments object and rest parameter</h3><p>There are three main differences between arguments object and rest parameters</p><ol><li>The arguments object is an array-like but not an array. Whereas the rest parameters are array instances.</li><li>The arguments object does not support methods such as sort, map, forEach, or pop. Whereas these methods can be used in rest parameters.</li><li>The rest parameters are only the ones that haven’t been given a separate name, while the arguments object contains all arguments passed to the function</li></ol><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-are-the-differences-between-spread-operator-and-rest-parameter"><a href="#What-are-the-differences-between-spread-operator-and-rest-parameter" class="headerlink" title="What are the differences between spread operator and rest parameter"></a>What are the differences between spread operator and rest parameter</h3><p>Rest parameter collects all remaining elements into an array. Whereas Spread operator allows iterables( arrays / objects / strings ) to be expanded into single arguments/elements. i.e, Rest parameter is opposite to the spread operator.</p><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-are-the-different-kinds-of-generators"><a href="#What-are-the-different-kinds-of-generators" class="headerlink" title="What are the different kinds of generators"></a>What are the different kinds of generators</h3><p>There are five kinds of generators,</p><ol><li><p><strong>Generator function declaration:</strong></p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span>* <span class="title">myGenFunc</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">     <span class="keyword">yield</span> <span class="number">1</span>;</span><br><span class="line">     <span class="keyword">yield</span> <span class="number">2</span>;</span><br><span class="line">     <span class="keyword">yield</span> <span class="number">3</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">const</span> genObj = myGenFunc();</span><br></pre></td></tr></table></figure></li><li><p><strong>Generator function expressions:</strong></p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> myGenFunc = <span class="function"><span class="keyword">function</span>* (<span class="params"></span>) </span>&#123;</span><br><span class="line">       <span class="keyword">yield</span> <span class="number">1</span>;</span><br><span class="line">       <span class="keyword">yield</span> <span class="number">2</span>;</span><br><span class="line">       <span class="keyword">yield</span> <span class="number">3</span>;</span><br><span class="line">&#125;;</span><br><span class="line"><span class="keyword">const</span> genObj = myGenFunc();</span><br></pre></td></tr></table></figure></li><li><p><strong>Generator method definitions in object literals:</strong></p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> myObj = &#123;</span><br><span class="line">    * <span class="function"><span class="title">myGeneratorMethod</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">       <span class="keyword">yield</span> <span class="number">1</span>;</span><br><span class="line">       <span class="keyword">yield</span> <span class="number">2</span>;</span><br><span class="line">       <span class="keyword">yield</span> <span class="number">3</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br><span class="line"><span class="keyword">const</span> genObj = myObj.myGeneratorMethod();</span><br></pre></td></tr></table></figure></li><li><p><strong>Generator method definitions in class:</strong></p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">MyClass</span> </span>&#123;</span><br><span class="line">   * <span class="function"><span class="title">myGeneratorMethod</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">      <span class="keyword">yield</span> <span class="number">1</span>;</span><br><span class="line">      <span class="keyword">yield</span> <span class="number">2</span>;</span><br><span class="line">      <span class="keyword">yield</span> <span class="number">3</span>;</span><br><span class="line">   &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">const</span> myObject = <span class="keyword">new</span> MyClass();</span><br><span class="line"><span class="keyword">const</span> genObj = myObject.myGeneratorMethod();</span><br></pre></td></tr></table></figure></li><li><p><strong>Generator as a computed property:</strong></p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> SomeObj = &#123;</span><br><span class="line">  *[<span class="built_in">Symbol</span>.iterator] () &#123;</span><br><span class="line">    <span class="keyword">yield</span> <span class="number">1</span>;</span><br><span class="line">    <span class="keyword">yield</span> <span class="number">2</span>;</span><br><span class="line">    <span class="keyword">yield</span> <span class="number">3</span>;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="built_in">console</span>.log(<span class="built_in">Array</span>.from(SomeObj)); <span class="comment">// [ 1, 2, 3 ]</span></span><br></pre></td></tr></table></figure></li></ol><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-are-the-built-in-iterables"><a href="#What-are-the-built-in-iterables" class="headerlink" title="What are the built-in iterables"></a>What are the built-in iterables</h3><p>Below are the list of built-in iterables in javascript,</p><ol><li>Arrays and TypedArrays</li><li>Strings: Iterate over each character or Unicode code-points</li><li>Maps: iterate over its key-value pairs</li><li>Sets: iterates over their elements</li><li>arguments: An array-like special variable in functions</li><li>DOM collection such as NodeList</li></ol><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-are-the-differences-between-for…of-and-for…in-statements"><a href="#What-are-the-differences-between-for…of-and-for…in-statements" class="headerlink" title="What are the differences between for…of and for…in statements"></a>What are the differences between for…of and for…in statements</h3><p>Both for…in and for…of statements iterate over js data structures. The only difference is over what they iterate:</p><ol><li>for..in iterates over all enumerable property keys of an object</li><li>for..of iterates over the values of an iterable object.</li></ol><p>Let’s explain this difference with an example,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> arr = [<span class="string">&#x27;a&#x27;</span>, <span class="string">&#x27;b&#x27;</span>, <span class="string">&#x27;c&#x27;</span>];</span><br><span class="line"></span><br><span class="line">arr.newProp = <span class="string">&#x27;newVlue&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">// key are the property keys</span></span><br><span class="line"><span class="keyword">for</span> (<span class="keyword">let</span> key <span class="keyword">in</span> arr) &#123;</span><br><span class="line">  <span class="built_in">console</span>.log(key);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// value are the property values</span></span><br><span class="line"><span class="keyword">for</span> (<span class="keyword">let</span> value <span class="keyword">of</span> arr) &#123;</span><br><span class="line">  <span class="built_in">console</span>.log(value);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>Since for..in loop iterates over the keys of the object, the first loop logs 0, 1, 2 and newProp while iterating over the array object. The for..of loop iterates over the values of a arr data structure and logs a, b, c in the console.</p><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="How-do-you-define-instance-and-non-instance-properties"><a href="#How-do-you-define-instance-and-non-instance-properties" class="headerlink" title="How do you define instance and non-instance properties"></a>How do you define instance and non-instance properties</h3><p>The Instance properties must be defined inside of class methods. For example, name and age properties defined insider constructor as below,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Person</span> </span>&#123;</span><br><span class="line">  <span class="function"><span class="title">constructor</span>(<span class="params">name, age</span>)</span> &#123;</span><br><span class="line">    <span class="built_in">this</span>.name = name;</span><br><span class="line">    <span class="built_in">this</span>.age = age;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>But Static(class) and prototype data properties must be defined outside of the ClassBody declaration. Let’s assign the age value for Person class as below,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">Person.staticAge = <span class="number">30</span>;</span><br><span class="line">Person.prototype.prototypeAge = <span class="number">40</span>;</span><br></pre></td></tr></table></figure><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></li><li><h3 id="What-is-the-difference-between-isNaN-and-Number-isNaN"><a href="#What-is-the-difference-between-isNaN-and-Number-isNaN" class="headerlink" title="What is the difference between isNaN and Number.isNaN?"></a>What is the difference between isNaN and Number.isNaN?</h3><ol><li><strong>isNaN</strong>: The global function <code>isNaN</code> converts the argument to a Number and returns true if the resulting value is NaN.</li><li><strong>Number.isNaN</strong>: This method does not convert the argument. But it returns true when the type is a Number and value is NaN.</li></ol><p>Let’s see the difference with an example,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">isNaN</span>(‘hello’);   <span class="comment">// true</span></span><br><span class="line"><span class="built_in">Number</span>.isNaN(<span class="string">&#x27;hello&#x27;</span>); <span class="comment">// false</span></span><br></pre></td></tr></table></figure><h3 id="Coding-Exercise"><a href="#Coding-Exercise" class="headerlink" title="Coding Exercise"></a>Coding Exercise</h3></li></ol><h4 id="1-What-is-the-output-of-below-code"><a href="#1-What-is-the-output-of-below-code" class="headerlink" title="1. What is the output of below code"></a>1. What is the output of below code</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> car = <span class="keyword">new</span> Vehicle(<span class="string">&quot;Honda&quot;</span>, <span class="string">&quot;white&quot;</span>, <span class="string">&quot;2010&quot;</span>, <span class="string">&quot;UK&quot;</span>);</span><br><span class="line"><span class="built_in">console</span>.log(car);</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">Vehicle</span>(<span class="params">model, color, year, country</span>) </span>&#123;</span><br><span class="line">    <span class="built_in">this</span>.model = model;</span><br><span class="line">    <span class="built_in">this</span>.color = color;</span><br><span class="line">    <span class="built_in">this</span>.year = year;</span><br><span class="line">    <span class="built_in">this</span>.country = country;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><ul><li>1: Undefined</li><li>2: ReferenceError</li><li>3: null</li><li>4: {model: “Honda”, color: “white”, year: “2010”, country: “UK”}</li></ul><details><summary><b>Answer</b></summary><p></p><h5 id="Answer-4"><a href="#Answer-4" class="headerlink" title="Answer: 4"></a>Answer: 4</h5><p>The function declarations are hoisted similar to any variables. So the placement for <code>Vehicle</code> function declaration doesn’t make any difference.</p><p></p></details><hr><h4 id="2-What-is-the-output-of-below-code"><a href="#2-What-is-the-output-of-below-code" class="headerlink" title="2. What is the output of below code"></a>2. What is the output of below code</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">foo</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="keyword">let</span> x = y = <span class="number">0</span>;</span><br><span class="line">    x++;</span><br><span class="line">    y++;</span><br><span class="line">    <span class="keyword">return</span> x;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="built_in">console</span>.log(foo(), <span class="keyword">typeof</span> x, <span class="keyword">typeof</span> y);</span><br></pre></td></tr></table></figure><ul><li>1: 1, undefined and undefined</li><li>2: ReferenceError: X is not defined</li><li>3: 1, undefined and number</li><li>4: 1, number and number</li></ul><details><summary><b>Answer</b></summary><p></p><h5 id="Answer-3"><a href="#Answer-3" class="headerlink" title="Answer: 3"></a>Answer: 3</h5><p>Of course the return value of <code>foo()</code> is 1 due to the increment operator. But the statement <code>let x = y = 0</code> declares a local variable x. Whereas y declared as a global variable accidentally. This statement is equivalent to,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> x;</span><br><span class="line"><span class="built_in">window</span>.y = <span class="number">0</span>;</span><br><span class="line">x = <span class="built_in">window</span>.y;</span><br></pre></td></tr></table></figure><p>Since the block scoped variable x is undefined outside of the function, the type will be undefined too. Whereas the global variable <code>y</code> is available outside the function, the value is 0 and type is number.</p><p></p></details><hr><h4 id="3-What-is-the-output-of-below-code"><a href="#3-What-is-the-output-of-below-code" class="headerlink" title="3. What is the output of below code"></a>3. What is the output of below code</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">main</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">   <span class="built_in">console</span>.log(<span class="string">&#x27;A&#x27;</span>);</span><br><span class="line">   <span class="built_in">setTimeout</span>(</span><br><span class="line">      <span class="function"><span class="keyword">function</span> <span class="title">print</span>(<span class="params"></span>)</span>&#123; <span class="built_in">console</span>.log(<span class="string">&#x27;B&#x27;</span>); &#125;</span><br><span class="line">   ,<span class="number">0</span>);</span><br><span class="line">   <span class="built_in">console</span>.log(<span class="string">&#x27;C&#x27;</span>);</span><br><span class="line">&#125;</span><br><span class="line">main();</span><br></pre></td></tr></table></figure><ul><li>1: A, B and C</li><li>2: B, A and C</li><li>3: A and C</li><li>4: A, C and B</li></ul><details><summary><b>Answer</b></summary><p></p><h5 id="Answer-4-1"><a href="#Answer-4-1" class="headerlink" title="Answer: 4"></a>Answer: 4</h5><p>The statements order is based on the event loop mechanism. The order of statements follows the below order,</p><ol><li>At first, the main function is pushed to the stack.</li><li>Then the browser pushes the fist statement of the main function( i.e, A’s console.log) to the stack, executing and popping out immediately.</li><li>But <code>setTimeout</code> statement moved to Browser API to apply the delay for callback.</li><li>In the meantime, C’s console.log added to stack, executed and popped out.</li><li>The callback of <code>setTimeout</code> moved from Browser API to message queue.</li><li>The <code>main</code> function popped out from stack because there are no statements to execute</li><li>The callback moved from message queue to the stack since the stack is empty.</li><li>The console.log for B is added to the stack and display on the console.</li></ol><p></p></details><hr><h4 id="4-What-is-the-output-of-below-equality-check"><a href="#4-What-is-the-output-of-below-equality-check" class="headerlink" title="4. What is the output of below equality check"></a>4. What is the output of below equality check</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">console</span>.log(<span class="number">0.1</span> + <span class="number">0.2</span> === <span class="number">0.3</span>);</span><br></pre></td></tr></table></figure><ul><li>1: false</li><li>2: true</li></ul><details><summary><b>Answer</b></summary><p></p><h5 id="Answer-1"><a href="#Answer-1" class="headerlink" title="Answer: 1"></a>Answer: 1</h5><p>This is due to the float point math problem. Since the floating point numbers are encoded in binary format, the addition operations on them lead to rounding errors. Hence, the comparison of floating points doesn’t give expected results.<br>You can find more details about the explanation here <a target="_blank" rel="noopener" href="https://0.30000000000000004.com/">0.30000000000000004.com/</a></p><p></p></details><hr><h4 id="5-What-is-the-output-of-below-code"><a href="#5-What-is-the-output-of-below-code" class="headerlink" title="5. What is the output of below code"></a>5. What is the output of below code</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> y = <span class="number">1</span>;</span><br><span class="line">  <span class="keyword">if</span> (<span class="function"><span class="keyword">function</span> <span class="title">f</span>(<span class="params"></span>)</span>&#123;&#125;) &#123;</span><br><span class="line">    y += <span class="keyword">typeof</span> f;</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="built_in">console</span>.log(y);</span><br></pre></td></tr></table></figure><ul><li>1: 1function</li><li>2: 1object</li><li>3: ReferenceError</li><li>4: 1undefined</li></ul><details><summary><b>Answer</b></summary><p></p><h5 id="Answer-4-2"><a href="#Answer-4-2" class="headerlink" title="Answer: 4"></a>Answer: 4</h5><p>The main points in the above code snippets are,</p><ol><li>You can see function expression instead function declaration inside if statement. So it always returns true.</li><li>Since it is not declared(or assigned) anywhere, f is undefined and typeof f is undefined too.</li></ol><p>In other words, it is same as</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> y = <span class="number">1</span>;</span><br><span class="line">  <span class="keyword">if</span> (<span class="string">&#x27;foo&#x27;</span>) &#123;</span><br><span class="line">    y += <span class="keyword">typeof</span> f;</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="built_in">console</span>.log(y);</span><br></pre></td></tr></table></figure><p><strong>Note:</strong> It returns 1object for MS Edge browser</p><p></p></details><hr><h4 id="6-What-is-the-output-of-below-code"><a href="#6-What-is-the-output-of-below-code" class="headerlink" title="6. What is the output of below code"></a>6. What is the output of below code</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">foo</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">  <span class="keyword">return</span></span><br><span class="line">  &#123;</span><br><span class="line">    message: <span class="string">&quot;Hello World&quot;</span></span><br><span class="line">  &#125;;</span><br><span class="line">&#125;</span><br><span class="line"><span class="built_in">console</span>.log(foo());</span><br></pre></td></tr></table></figure><ul><li>1: Hello World</li><li>2: Object {message: “Hello World”}</li><li>3: Undefined</li><li>4: SyntaxError</li></ul><details><summary><b>Answer</b></summary><p></p><h5 id="Answer-3-1"><a href="#Answer-3-1" class="headerlink" title="Answer: 3"></a>Answer: 3</h5><p>This is a semicolon issue. Normally semicolons are optional in JavaScript. So if there are any statements(in this case, return) missing semicolon, it is automatically inserted immediately. Hence, the function returned as undefined.</p><p>Whereas if the opening curly brace is along with the return keyword then the function is going to be returned as expected.</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">foo</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">  <span class="keyword">return</span> &#123;</span><br><span class="line">    message: <span class="string">&quot;Hello World&quot;</span></span><br><span class="line">  &#125;;</span><br><span class="line">&#125;</span><br><span class="line"><span class="built_in">console</span>.log(foo()); <span class="comment">// &#123;message: &quot;Hello World&quot;&#125;</span></span><br></pre></td></tr></table></figure><p></p></details><hr><h4 id="7-What-is-the-output-of-below-code"><a href="#7-What-is-the-output-of-below-code" class="headerlink" title="7. What is the output of below code"></a>7. What is the output of below code</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> myChars = [<span class="string">&#x27;a&#x27;</span>, <span class="string">&#x27;b&#x27;</span>, <span class="string">&#x27;c&#x27;</span>, <span class="string">&#x27;d&#x27;</span>]</span><br><span class="line"><span class="keyword">delete</span> myChars[<span class="number">0</span>];</span><br><span class="line"><span class="built_in">console</span>.log(myChars);</span><br><span class="line"><span class="built_in">console</span>.log(myChars[<span class="number">0</span>]);</span><br><span class="line"><span class="built_in">console</span>.log(myChars.length);</span><br></pre></td></tr></table></figure><ul><li>1: [empty, ‘b’, ‘c’, ‘d’], empty, 3</li><li>2: [null, ‘b’, ‘c’, ‘d’], empty, 3</li><li>3: [empty, ‘b’, ‘c’, ‘d’], undefined, 4</li><li>4: [null, ‘b’, ‘c’, ‘d’], undefined, 4</li></ul><details><summary><b>Answer</b></summary><p></p><h5 id="Answer-3-2"><a href="#Answer-3-2" class="headerlink" title="Answer: 3"></a>Answer: 3</h5><p>The <code>delete</code> operator will delete the object property but it will not reindex the array or change its length. So the number or elements or length of the array won’t be changed.<br>If you try to print myChars then you can observe that it doesn’t set an undefined value, rather the property is removed from the array. The newer versions of Chrome use <code>empty</code> instead of <code>undefined</code> to make the difference a bit clearer.</p><p></p></details><hr><h4 id="8-What-is-the-output-of-below-code-in-latest-Chrome"><a href="#8-What-is-the-output-of-below-code-in-latest-Chrome" class="headerlink" title="8. What is the output of below code in latest Chrome"></a>8. What is the output of below code in latest Chrome</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> array1 = <span class="keyword">new</span> <span class="built_in">Array</span>(<span class="number">3</span>);</span><br><span class="line"><span class="built_in">console</span>.log(array1);</span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> array2 = [];</span><br><span class="line">array2[<span class="number">2</span>] = <span class="number">100</span>;</span><br><span class="line"><span class="built_in">console</span>.log(array2);</span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> array3 = [,,,];</span><br><span class="line"><span class="built_in">console</span>.log(array3);</span><br></pre></td></tr></table></figure><ul><li>1: [undefined × 3], [undefined × 2, 100], [undefined × 3]</li><li>2: [empty × 3], [empty × 2, 100], [empty × 3]</li><li>3: [null × 3], [null × 2, 100], [null × 3]</li><li>4: [], [100], []</li></ul><details><summary><b>Answer</b></summary><p></p><h5 id="Answer-2"><a href="#Answer-2" class="headerlink" title="Answer: 2"></a>Answer: 2</h5><p>The latest chrome versions display <code>sparse array</code>(they are filled with holes) using this empty x n notation. Whereas the older versions have undefined x n notation.<br><strong>Note:</strong> The latest version of FF displays <code>n empty slots</code> notation.</p><p></p></details><hr><h4 id="9-What-is-the-output-of-below-code"><a href="#9-What-is-the-output-of-below-code" class="headerlink" title="9. What is the output of below code"></a>9. What is the output of below code</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> obj = &#123;</span><br><span class="line">  prop1: <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123; <span class="keyword">return</span> <span class="number">0</span> &#125;,</span><br><span class="line">  <span class="function"><span class="title">prop2</span>(<span class="params"></span>)</span> &#123; <span class="keyword">return</span> <span class="number">1</span> &#125;,</span><br><span class="line">  [<span class="string">&#x27;prop&#x27;</span> + <span class="number">3</span>]() &#123; <span class="keyword">return</span> <span class="number">2</span> &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="built_in">console</span>.log(obj.prop1());</span><br><span class="line"><span class="built_in">console</span>.log(obj.prop2());</span><br><span class="line"><span class="built_in">console</span>.log(obj.prop3());</span><br></pre></td></tr></table></figure><ul><li>1: 0, 1, 2</li><li>2: 0, { return 1 }, 2</li><li>3: 0, { return 1 }, { return 2 }</li><li>4: 0, 1, undefined</li></ul><details><summary><b>Answer</b></summary><p></p><h5 id="Answer-1-1"><a href="#Answer-1-1" class="headerlink" title="Answer: 1"></a>Answer: 1</h5><p>ES6 provides method definitions and property shorthands for objects. So both prop2 and prop3 are treated as regular function values.</p><p></p></details><hr><h4 id="10-What-is-the-output-of-below-code"><a href="#10-What-is-the-output-of-below-code" class="headerlink" title="10. What is the output of below code"></a>10. What is the output of below code</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">console</span>.log(<span class="number">1</span> &lt; <span class="number">2</span> &lt; <span class="number">3</span>);</span><br><span class="line"><span class="built_in">console</span>.log(<span class="number">3</span> &gt; <span class="number">2</span> &gt; <span class="number">1</span>);</span><br></pre></td></tr></table></figure><ul><li>1: true, true</li><li>2: true, false</li><li>3: SyntaxError, SyntaxError,</li><li>4: false, false</li></ul><details><summary><b>Answer</b></summary><p></p><h5 id="Answer-2-1"><a href="#Answer-2-1" class="headerlink" title="Answer: 2"></a>Answer: 2</h5><p>The important point is that if the statement contains the same operators(e.g, &lt; or &gt;) then it can be evaluated from left to right.<br>The first statement follows the below order,</p><ol><li>console.log(1 &lt; 2 &lt; 3);</li><li>console.log(true &lt; 3);</li><li>console.log(1 &lt; 3); // True converted as <code>1</code> during comparison</li><li>True</li></ol><p>Whereas the second statement follows the below order,</p><ol><li>console.log(3 &gt; 2 &gt; 1);</li><li>console.log(true &gt; 1);</li><li>console.log(1 &gt; 1); // False converted as <code>0</code> during comparison</li><li>False</li></ol><p></p></details><hr><h4 id="11-What-is-the-output-of-below-code-in-non-strict-mode"><a href="#11-What-is-the-output-of-below-code-in-non-strict-mode" class="headerlink" title="11. What is the output of below code in non-strict mode"></a>11. What is the output of below code in non-strict mode</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">printNumbers</span>(<span class="params">first, second, first</span>) </span>&#123;</span><br><span class="line">  <span class="built_in">console</span>.log(first, second, first);</span><br><span class="line">&#125;</span><br><span class="line">printNumbers(<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>);</span><br></pre></td></tr></table></figure><ul><li>1: 1, 2, 3</li><li>2: 3, 2, 3</li><li>3: SyntaxError: Duplicate parameter name not allowed in this context</li><li>4: 1, 2, 1</li></ul><details><summary><b>Answer</b></summary><p></p><h5 id="Answer-2-2"><a href="#Answer-2-2" class="headerlink" title="Answer: 2"></a>Answer: 2</h5><p>In non-strict mode, the regular JavaScript functions allow duplicate named parameters. The above code snippet has duplicate parameters on 1st and 3rd parameters.<br>The value of the first parameter is mapped to the third argument which is passed to the function. Hence, the 3rd argument overrides the first parameter.</p><p><strong>Note:</strong> In strict mode, duplicate parameters will throw a Syntax Error.</p><p></p></details><hr><h4 id="12-What-is-the-output-of-below-code"><a href="#12-What-is-the-output-of-below-code" class="headerlink" title="12. What is the output of below code"></a>12. What is the output of below code</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> printNumbersArrow = <span class="function">(<span class="params">first, second, first</span>) =&gt;</span> &#123;</span><br><span class="line">  <span class="built_in">console</span>.log(first, second, first);</span><br><span class="line">&#125;</span><br><span class="line">printNumbersArrow(<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>);</span><br></pre></td></tr></table></figure><ul><li>1: 1, 2, 3</li><li>2: 3, 2, 3</li><li>3: SyntaxError: Duplicate parameter name not allowed in this context</li><li>4: 1, 2, 1</li></ul><details><summary><b>Answer</b></summary><p></p><h5 id="Answer-3-3"><a href="#Answer-3-3" class="headerlink" title="Answer: 3"></a>Answer: 3</h5><p>Unlike regular functions, the arrow functions doesn’t not allow duplicate parameters in either strict or non-strict mode. So you can see <code>SyntaxError</code> in the console.</p><p></p></details><hr><h4 id="13-What-is-the-output-of-below-code"><a href="#13-What-is-the-output-of-below-code" class="headerlink" title="13. What is the output of below code"></a>13. What is the output of below code</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> arrowFunc = <span class="function">() =&gt;</span> <span class="built_in">arguments</span>.length;</span><br><span class="line"><span class="built_in">console</span>.log(arrowFunc(<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>));</span><br></pre></td></tr></table></figure><ul><li>1: ReferenceError: arguments is not defined</li><li>2: 3</li><li>3: undefined</li><li>4: null</li></ul><details><summary><b>Answer</b></summary><p></p><h5 id="Answer-1-2"><a href="#Answer-1-2" class="headerlink" title="Answer: 1"></a>Answer: 1</h5><p>Arrow functions do not have an <code>arguments, super, this, or new.target</code> bindings. So any reference to <code>arguments</code> variable tries to resolve to a binding in a lexically enclosing environment. In this case, the arguments variable is not defined outside of the arrow function. Hence, you will receive a reference error.</p><p>Where as the normal function provides the number of arguments passed to the function</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> func = <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">                    <span class="keyword">return</span> <span class="built_in">arguments</span>.length;</span><br><span class="line">                    &#125;</span><br><span class="line"><span class="built_in">console</span>.log(func(<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>));</span><br></pre></td></tr></table></figure><p>But If you still want to use an arrow function then rest operator on arguments provides the expected arguments</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> arrowFunc = <span class="function">(<span class="params">...args</span>) =&gt;</span> args.length;</span><br><span class="line"><span class="built_in">console</span>.log(arrowFunc(<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>));</span><br></pre></td></tr></table></figure><p></p></details><hr><h4 id="14-What-is-the-output-of-below-code"><a href="#14-What-is-the-output-of-below-code" class="headerlink" title="14. What is the output of below code"></a>14. What is the output of below code</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">console</span>.log( <span class="built_in">String</span>.prototype.trimLeft.name === <span class="string">&#x27;trimLeft&#x27;</span> );</span><br><span class="line"><span class="built_in">console</span>.log( <span class="built_in">String</span>.prototype.trimLeft.name === <span class="string">&#x27;trimStart&#x27;</span> );</span><br></pre></td></tr></table></figure><ul><li>1: True, False</li><li>2: False, True</li></ul><details><summary><b>Answer</b></summary><p></p><h5 id="Answer-2-3"><a href="#Answer-2-3" class="headerlink" title="Answer: 2"></a>Answer: 2</h5><p>In order to be consistent with functions like <code>String.prototype.padStart</code>, the standard method name for trimming the whitespaces is considered as <code>trimStart</code>. Due to web web compatibility reasons, the old method name ‘trimLeft’ still acts as an alias for ‘trimStart’. Hence, the prototype for ‘trimLeft’ is always ‘trimStart’</p><p></p></details><hr><h4 id="15-What-is-the-output-of-below-code"><a href="#15-What-is-the-output-of-below-code" class="headerlink" title="15. What is the output of below code"></a>15. What is the output of below code</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">console</span>.log(<span class="built_in">Math</span>.max());</span><br></pre></td></tr></table></figure><ul><li>1: undefined</li><li>2: Infinity</li><li>3: 0</li><li>4: -Infinity</li></ul><details><summary><b>Answer</b></summary><p></p><h5 id="Answer-4-3"><a href="#Answer-4-3" class="headerlink" title="Answer: 4"></a>Answer: 4</h5><p>-Infinity is the initial comparant because almost every other value is bigger. So when no arguments are provided, -Infinity is going to be returned.<br><strong>Note:</strong> Zero number of arguments is a valid case.</p><p></p></details><hr><h4 id="16-What-is-the-output-of-below-code"><a href="#16-What-is-the-output-of-below-code" class="headerlink" title="16. What is the output of below code"></a>16. What is the output of below code</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">console</span>.log(<span class="number">10</span> == [<span class="number">10</span>]);</span><br><span class="line"><span class="built_in">console</span>.log(<span class="number">10</span> == [[[[[[[<span class="number">10</span>]]]]]]]);</span><br></pre></td></tr></table></figure><ul><li>1: True, True</li><li>2: True, False</li><li>3: False, False</li><li>4: False, True</li></ul><details><summary><b>Answer</b></summary><p></p><h5 id="Answer-1-3"><a href="#Answer-1-3" class="headerlink" title="Answer: 1"></a>Answer: 1</h5><p>As per the comparison algorithm in the ECMAScript specification(ECMA-262), the above expression converted into JS as below</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">10</span> === <span class="built_in">Number</span>([<span class="number">10</span>].valueOf().toString()) <span class="comment">// 10</span></span><br></pre></td></tr></table></figure><p>So it doesn’t matter about number brackets([]) around the number, it is always converted to a number in the expression.</p><p></p></details><hr><h4 id="17-What-is-the-output-of-below-code"><a href="#17-What-is-the-output-of-below-code" class="headerlink" title="17. What is the output of below code"></a>17. What is the output of below code</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">console</span>.log(<span class="number">10</span> + <span class="string">&#x27;10&#x27;</span>);</span><br><span class="line"><span class="built_in">console</span>.log(<span class="number">10</span> - <span class="string">&#x27;10&#x27;</span>);</span><br></pre></td></tr></table></figure><ul><li>1: 20, 0</li><li>2: 1010, 0</li><li>3: 1010, 10-10</li><li>4: NaN, NaN</li></ul><details><summary><b>Answer</b></summary><p></p><h5 id="Answer-2-4"><a href="#Answer-2-4" class="headerlink" title="Answer: 2"></a>Answer: 2</h5><p>The concatenation operator(+) is applicable for both number and string types. So if any operand is string type then both operands concatenated as strings. Whereas subtract(-) operator tries to convert the operands as number type.</p><p></p></details><hr><h4 id="18-What-is-the-output-of-below-code"><a href="#18-What-is-the-output-of-below-code" class="headerlink" title="18. What is the output of below code"></a>18. What is the output of below code</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">console</span>.log([<span class="number">0</span>] == <span class="literal">false</span>);</span><br><span class="line"><span class="keyword">if</span>([<span class="number">0</span>]) &#123;</span><br><span class="line"><span class="built_in">console</span>.log(<span class="string">&quot;I&#x27;m True&quot;</span>);</span><br><span class="line">&#125; <span class="keyword">else</span> &#123;</span><br><span class="line"><span class="built_in">console</span>.log(<span class="string">&quot;I&#x27;m False&quot;</span>);</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure><ul><li>1: True, I’m True</li><li>2: True, I’m False</li><li>3: False, I’m True</li><li>4: False, I’m False</li></ul><details><summary><b>Answer</b></summary><p></p><h5 id="Answer-1-4"><a href="#Answer-1-4" class="headerlink" title="Answer: 1"></a>Answer: 1</h5><p>In comparison operators, the expression <code>[0]</code> converted to Number([0].valueOf().toString()) which is resolved to false. Whereas <code>[0]</code> just becomes a truthy value without any conversion because there is no comparison operator.</p><p></p></details><h4 id="19-What-is-the-output-of-below-code"><a href="#19-What-is-the-output-of-below-code" class="headerlink" title="19. What is the output of below code"></a>19. What is the output of below code</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">console</span>.log([<span class="number">1</span>, <span class="number">2</span>] + [<span class="number">3</span>, <span class="number">4</span>]);</span><br></pre></td></tr></table></figure><ul><li>1: [1,2,3,4]</li><li>2: [1,2][3,4]</li><li>3: SyntaxError</li><li>4: 1,23,4</li></ul><details><summary><b>Answer</b></summary><p></p><h5 id="Answer-4-4"><a href="#Answer-4-4" class="headerlink" title="Answer: 4"></a>Answer: 4</h5><p>The + operator is not meant or defined for arrays. So it converts arrays into strings and concatenates them.</p><p></p></details><hr><h4 id="20-What-is-the-output-of-below-code"><a href="#20-What-is-the-output-of-below-code" class="headerlink" title="20. What is the output of below code"></a>20. What is the output of below code</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> numbers = <span class="keyword">new</span> <span class="built_in">Set</span>([<span class="number">1</span>, <span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>]);</span><br><span class="line"><span class="built_in">console</span>.log(numbers);</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> browser = <span class="keyword">new</span> <span class="built_in">Set</span>(<span class="string">&#x27;Firefox);</span></span><br><span class="line"><span class="string">console.log(browser);</span></span><br></pre></td></tr></table></figure><ul><li>1: {1, 2, 3, 4}, {“F”, “i”, “r”, “e”, “f”, “o”, “x”}</li><li>2: {1, 2, 3, 4}, {“F”, “i”, “r”, “e”, “o”, “x”}</li><li>3: [1, 2, 3, 4], [“F”, “i”, “r”, “e”, “o”, “x”]</li><li>4: {1, 1, 2, 3, 4}, {“F”, “i”, “r”, “e”, “f”, “o”, “x”}</li></ul><details><summary><b>Answer</b></summary><p></p><h5 id="Answer-1-5"><a href="#Answer-1-5" class="headerlink" title="Answer: 1"></a>Answer: 1</h5><p>Since <code>Set</code> object is a collection of unique values, it won’t allow duplicate values in the collection. At the same time, it is case sensitive data structure.</p><p></p></details><hr><h4 id="21-What-is-the-output-of-below-code"><a href="#21-What-is-the-output-of-below-code" class="headerlink" title="21. What is the output of below code"></a>21. What is the output of below code</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">console</span>.log(<span class="literal">NaN</span> === <span class="literal">NaN</span>);</span><br></pre></td></tr></table></figure><ul><li>1: True</li><li>2: False</li></ul><details><summary><b>Answer</b></summary><p></p><h5 id="Answer-2-5"><a href="#Answer-2-5" class="headerlink" title="Answer: 2"></a>Answer: 2</h5><p>JavaScript follows IEEE 754 spec standards. As per this spec, NaNs are never equal for floating-point numbers.</p><p></p></details><hr><h4 id="22-What-is-the-output-of-below-code"><a href="#22-What-is-the-output-of-below-code" class="headerlink" title="22. What is the output of below code"></a>22. What is the output of below code</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> numbers = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="literal">NaN</span>];</span><br><span class="line"><span class="built_in">console</span>.log(numbers.indexOf(<span class="literal">NaN</span>));</span><br></pre></td></tr></table></figure><ul><li>1: 4</li><li>2: NaN</li><li>3: SyntaxError</li><li>4: -1</li></ul><details><summary><b>Answer</b></summary><p></p><h5 id="Answer-4-5"><a href="#Answer-4-5" class="headerlink" title="Answer: 4"></a>Answer: 4</h5><p>The <code>indexOf</code> uses strict equality operator(===) internally and <code>NaN === NaN</code> evaluates to false. Since indexOf won’t be able to find NaN inside an array, it returns -1 always.<br>But you can use <code>Array.prototype.findIndex</code> method to find out the index of NaN in an array or You can use <code>Array.prototype.includes</code> to check if NaN is present in an array or not.</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> numbers = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="literal">NaN</span>];</span><br><span class="line"><span class="built_in">console</span>.log(numbers.findIndex(<span class="built_in">Number</span>.isNaN)); <span class="comment">// 4</span></span><br><span class="line"></span><br><span class="line"><span class="built_in">console</span>.log(numbers.includes(<span class="built_in">Number</span>.isNaN)); <span class="comment">// true</span></span><br></pre></td></tr></table></figure><p></p></details><hr><h4 id="23-What-is-the-output-of-below-code"><a href="#23-What-is-the-output-of-below-code" class="headerlink" title="23. What is the output of below code"></a>23. What is the output of below code</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> [a, ...b,] = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>];</span><br><span class="line"><span class="built_in">console</span>.log(a, b);</span><br></pre></td></tr></table></figure><ul><li>1: 1, [2, 3, 4, 5]</li><li>2: 1, {2, 3, 4, 5}</li><li>3: SyntaxError</li><li>4: 1, [2, 3, 4]</li></ul><details><summary><b>Answer</b></summary><p></p><h5 id="Answer-3-4"><a href="#Answer-3-4" class="headerlink" title="Answer: 3"></a>Answer: 3</h5><p>When using rest parameters, trailing commas are not allowed and will throw a SyntaxError.<br>If you remove the trailing comma then it displays 1st answer</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> [a, ...b,] = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>];</span><br><span class="line"><span class="built_in">console</span>.log(a, b); <span class="comment">// 1, [2, 3, 4, 5]</span></span><br></pre></td></tr></table></figure><p></p></details><hr><h4 id="25-What-is-the-output-of-below-code"><a href="#25-What-is-the-output-of-below-code" class="headerlink" title="25. What is the output of below code"></a>25. What is the output of below code</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">async</span> <span class="function"><span class="keyword">function</span> <span class="title">func</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">   <span class="keyword">return</span> <span class="number">10</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="built_in">console</span>.log(func());</span><br></pre></td></tr></table></figure><ul><li>1: Promise {<resolved>: 10}</resolved></li><li>2: 10</li><li>3: SyntaxError</li><li>4: Promise {<rejected>: 10}</rejected></li></ul><details><summary><b>Answer</b></summary><p></p><h5 id="Answer-1-6"><a href="#Answer-1-6" class="headerlink" title="Answer: 1"></a>Answer: 1</h5><p>Async functions always return a promise. But even if the return value of an async function is not explicitly a promise, it will be implicitly wrapped in a promise. The above async function is equivalent to below expression,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">func</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">   <span class="keyword">return</span> <span class="built_in">Promise</span>.resolve(<span class="number">10</span>)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p></p></details><hr><h4 id="26-What-is-the-output-of-below-code"><a href="#26-What-is-the-output-of-below-code" class="headerlink" title="26. What is the output of below code"></a>26. What is the output of below code</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">async</span> <span class="function"><span class="keyword">function</span> <span class="title">func</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">   <span class="keyword">await</span> <span class="number">10</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="built_in">console</span>.log(func());</span><br></pre></td></tr></table></figure><ul><li>1: Promise {<resolved>: 10}</resolved></li><li>2: 10</li><li>3: SyntaxError</li><li>4: Promise {<resolved>: undefined}</resolved></li></ul><details><summary><b>Answer</b></summary><p></p><h5 id="Answer-4-6"><a href="#Answer-4-6" class="headerlink" title="Answer: 4"></a>Answer: 4</h5><p>The await expression returns value 10 with promise resolution and the code after each await expression can be treated as existing in a <code>.then</code> callback. In this case, there is no return expression at the end of the function. Hence, the default return value of <code>undefined</code> is returned as the resolution of the promise. The above async function is equivalent to below expression,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">func</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">   <span class="keyword">return</span> <span class="built_in">Promise</span>.resolve(<span class="number">10</span>).then(<span class="function">() =&gt;</span> <span class="literal">undefined</span>)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p></p></details><hr><h4 id="27-What-is-the-output-of-below-code"><a href="#27-What-is-the-output-of-below-code" class="headerlink" title="27. What is the output of below code"></a>27. What is the output of below code</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">delay</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">  <span class="keyword">return</span> <span class="keyword">new</span> <span class="built_in">Promise</span>(<span class="function"><span class="params">resolve</span> =&gt;</span> <span class="built_in">setTimeout</span>(resolve, <span class="number">2000</span>));</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">async</span> <span class="function"><span class="keyword">function</span> <span class="title">delayedLog</span>(<span class="params">item</span>) </span>&#123;</span><br><span class="line">  <span class="keyword">await</span> delay();</span><br><span class="line">  <span class="built_in">console</span>.log(item);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">async</span> <span class="function"><span class="keyword">function</span> <span class="title">processArray</span>(<span class="params">array</span>) </span>&#123;</span><br><span class="line">  array.forEach(<span class="function"><span class="params">item</span> =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">await</span> delayedLog(item);</span><br><span class="line">  &#125;)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">processArray([<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>]);</span><br></pre></td></tr></table></figure><ul><li>1: SyntaxError</li><li>2: 1, 2, 3, 4</li><li>3: 4, 4, 4, 4</li><li>4: 4, 3, 2, 1</li></ul><details><summary><b>Answer</b></summary><p></p><h5 id="Answer-1-7"><a href="#Answer-1-7" class="headerlink" title="Answer: 1"></a>Answer: 1</h5><p>Even though “processArray” is an async function, the anonymous function that we use for <code>forEach</code> is synchronous. If you use await inside a synchronous function then it throws a syntax error.</p><p></p></details><hr><h4 id="28-What-is-the-output-of-below-code"><a href="#28-What-is-the-output-of-below-code" class="headerlink" title="28. What is the output of below code"></a>28. What is the output of below code</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">delay</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">  <span class="keyword">return</span> <span class="keyword">new</span> <span class="built_in">Promise</span>(<span class="function"><span class="params">resolve</span> =&gt;</span> <span class="built_in">setTimeout</span>(resolve, <span class="number">2000</span>));</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">async</span> <span class="function"><span class="keyword">function</span> <span class="title">delayedLog</span>(<span class="params">item</span>) </span>&#123;</span><br><span class="line">  <span class="keyword">await</span> delay();</span><br><span class="line">  <span class="built_in">console</span>.log(item);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">async</span> <span class="function"><span class="keyword">function</span> <span class="title">process</span>(<span class="params">array</span>) </span>&#123;</span><br><span class="line">  array.forEach(<span class="keyword">async</span> (item) =&gt; &#123;</span><br><span class="line">    <span class="keyword">await</span> delayedLog(i);</span><br><span class="line">  &#125;);</span><br><span class="line">  <span class="built_in">console</span>.log(<span class="string">&#x27;Process completed!&#x27;</span>);</span><br><span class="line">&#125;</span><br><span class="line">process([<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">5</span>]);</span><br></pre></td></tr></table></figure><ul><li>1: 1 2 3 5 and Process completed!</li><li>2: 5 5 5 5 and Process completed!</li><li>3: Process completed! and 5 5 5 5</li><li>4: Process completed! and 1 2 3 5</li></ul><details><summary><b>Answer</b></summary><p></p><h5 id="Answer-4-7"><a href="#Answer-4-7" class="headerlink" title="Answer: 4"></a>Answer: 4</h5><p>The forEach method will not wait until all items are finished but it just runs the tasks and goes next. Hence, the last statement is displayed first followed by a sequence of promise resolutions.</p><p>But you control the array sequence using for..of loop,</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">async</span> <span class="function"><span class="keyword">function</span> <span class="title">processArray</span>(<span class="params">array</span>) </span>&#123;</span><br><span class="line">  <span class="keyword">for</span> (<span class="keyword">const</span> item <span class="keyword">of</span> array) &#123;</span><br><span class="line">    <span class="keyword">await</span> delayedLog(item);</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="built_in">console</span>.log(<span class="string">&#x27;Process completed!&#x27;</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p></p></details><hr><h4 id="29-What-is-the-output-of-below-code"><a href="#29-What-is-the-output-of-below-code" class="headerlink" title="29. What is the output of below code"></a>29. What is the output of below code</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> set = <span class="keyword">new</span> <span class="built_in">Set</span>();</span><br><span class="line">set.add(<span class="string">&quot;+0&quot;</span>).add(<span class="string">&quot;-0&quot;</span>).add(<span class="literal">NaN</span>).add(<span class="literal">undefined</span>).add(<span class="literal">NaN</span>);;</span><br><span class="line"><span class="built_in">console</span>.log(set);</span><br></pre></td></tr></table></figure><ul><li>1: Set(4) {“+0”, “-0”, NaN, undefined}</li><li>2: Set(3) {“+0”, NaN, undefined}</li><li>3: Set(5) {“+0”, “-0”, NaN, undefined, NaN}</li><li>4: Set(4) {“+0”, NaN, undefined, NaN}</li></ul><details><summary><b>Answer</b></summary><p></p><h5 id="Answer-1-8"><a href="#Answer-1-8" class="headerlink" title="Answer: 1"></a>Answer: 1</h5><p>Set has few exceptions from equality check,</p><ol><li>All NaN values are equal</li><li>Both +0 and -0 considered as different values</li></ol><p></p></details><hr><h4 id="30-What-is-the-output-of-below-code"><a href="#30-What-is-the-output-of-below-code" class="headerlink" title="30. What is the output of below code"></a>30. What is the output of below code</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> sym1 = <span class="built_in">Symbol</span>(<span class="string">&#x27;one&#x27;</span>);</span><br><span class="line"><span class="keyword">const</span> sym2 = <span class="built_in">Symbol</span>(<span class="string">&#x27;one&#x27;</span>);</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> sym3 = <span class="built_in">Symbol</span>.for(<span class="string">&#x27;two&#x27;</span>);</span><br><span class="line"><span class="keyword">const</span> sym4 = <span class="built_in">Symbol</span>.for(<span class="string">&#x27;two&#x27;</span>);</span><br><span class="line"></span><br><span class="line">cnsooe.log(sym1 === sym2, sym3 === sym4);</span><br></pre></td></tr></table></figure><ul><li>1: true, true</li><li>2: true, false</li><li>3: false, true</li><li>4: false, false</li></ul><details><summary><b>Answer</b></summary><p></p><h5 id="Answer-3-5"><a href="#Answer-3-5" class="headerlink" title="Answer: 3"></a>Answer: 3</h5><p>Symbol follows below conventions,</p><ol><li>Every symbol value returned from Symbol() is unique irrespective of the optional string.</li><li><code>Symbol.for()</code> function creates a symbol in a global symbol registry list. But it doesn’t necessarily create a new symbol on every call, it checks first if a symbol with the given key is already present in the registry and returns the symbol if it is found. Otherwise a new symbol created in the registry.</li></ol><p><strong>Note:</strong> The symbol description is just useful for debugging purposes.</p><p></p></details><hr><h4 id="31-What-is-the-output-of-below-code"><a href="#31-What-is-the-output-of-below-code" class="headerlink" title="31. What is the output of below code"></a>31. What is the output of below code</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> sym1 = <span class="keyword">new</span> <span class="built_in">Symbol</span>(<span class="string">&#x27;one&#x27;</span>);</span><br><span class="line"><span class="built_in">console</span>.log(sym1);</span><br></pre></td></tr></table></figure><ul><li>1: SyntaxError</li><li>2: one</li><li>3: Symbol(‘one’)</li><li>4: Symbol</li></ul><details><summary><b>Answer</b></summary><p></p><h5 id="Answer-1-9"><a href="#Answer-1-9" class="headerlink" title="Answer: 1"></a>Answer: 1</h5><p><code>Symbol</code> is a just a standard function and not an object constructor(unlike other primitives new Boolean, new String and new Number). So if you try to call it with the new operator will result in a TypeError</p><p></p></details><hr><h4 id="32-What-is-the-output-of-below-code"><a href="#32-What-is-the-output-of-below-code" class="headerlink" title="32. What is the output of below code"></a>32. What is the output of below code</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> myNumber = <span class="number">100</span>;</span><br><span class="line"><span class="keyword">let</span> myString = <span class="string">&#x27;100&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> (!<span class="keyword">typeof</span> myNumber === <span class="string">&quot;string&quot;</span>) &#123;</span><br><span class="line">   <span class="built_in">console</span>.log(<span class="string">&quot;It is not a string!&quot;</span>);</span><br><span class="line">&#125; <span class="keyword">else</span> &#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">&quot;It is a string!&quot;</span>);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> (!<span class="keyword">typeof</span> myString === <span class="string">&quot;number&quot;</span>)&#123;</span><br><span class="line">   <span class="built_in">console</span>.log(<span class="string">&quot;It is not a number!&quot;</span>)</span><br><span class="line">&#125; <span class="keyword">else</span> &#123;</span><br><span class="line">   <span class="built_in">console</span>.log(<span class="string">&quot;It is a number!&quot;</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><ul><li>1: SyntaxError</li><li>2: It is not a string!, It is not a number!</li><li>3: It is not a string!, It is a number!</li><li>4: It is a string!, It is a number!</li></ul><details><summary><b>Answer</b></summary><p></p><h5 id="Answer-4-8"><a href="#Answer-4-8" class="headerlink" title="Answer: 4"></a>Answer: 4</h5><p>The return value of <code>typeof myNumber (OR) typeof myString</code> is always the truthy value (either “number” or “string”). Since ! operator converts the value to a boolean value, the value of both <code>!typeof myNumber or !typeof myString</code> is always false. Hence the if condition fails and control goes to else block.</p><p></p></details><hr><h4 id="33-What-is-the-output-of-below-code"><a href="#33-What-is-the-output-of-below-code" class="headerlink" title="33. What is the output of below code"></a>33. What is the output of below code</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">console</span>.log(<span class="built_in">JSON</span>.stringify(&#123; <span class="attr">myArray</span>: [<span class="string">&#x27;one&#x27;</span>, <span class="literal">undefined</span>, <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;&#125;, <span class="built_in">Symbol</span>(<span class="string">&#x27;&#x27;</span>)] &#125;));</span><br><span class="line"><span class="built_in">console</span>.log(<span class="built_in">JSON</span>.stringify(&#123; [<span class="built_in">Symbol</span>.for(<span class="string">&#x27;one&#x27;</span>)]: <span class="string">&#x27;one&#x27;</span> &#125;, [<span class="built_in">Symbol</span>.for(<span class="string">&#x27;one&#x27;</span>)]));</span><br></pre></td></tr></table></figure><ul><li>1: {“myArray”:[‘one’, undefined, {}, Symbol]}, {}</li><li>2: {“myArray”:[‘one’, null,null,null]}, {}</li><li>3: {“myArray”:[‘one’, null,null,null]}, “{ [Symbol.for(‘one’)]: ‘one’ }, [Symbol.for(‘one’)]”</li><li>4: {“myArray”:[‘one’, undefined, function(){}, Symbol(‘’)]}, {}</li></ul><details><summary><b>Answer</b></summary><p></p><h5 id="Answer-2-6"><a href="#Answer-2-6" class="headerlink" title="Answer: 2"></a>Answer: 2</h5><p>The symbols has below constraints,</p><ol><li>The undefined, Functions, and Symbols are not valid JSON values. So those values are either omitted (in an object) or changed to null (in an array). Hence, it returns null values for the value array.</li><li>All Symbol-keyed properties will be completely ignored. Hence it returns an empty object({}).</li></ol><p></p></details><hr><h4 id="34-What-is-the-output-of-below-code"><a href="#34-What-is-the-output-of-below-code" class="headerlink" title="34. What is the output of below code"></a>34. What is the output of below code</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">A</span> </span>&#123;</span><br><span class="line">  <span class="function"><span class="title">constructor</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="keyword">new</span>.target.name)</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">B</span> <span class="keyword">extends</span> <span class="title">A</span> </span>&#123; <span class="function"><span class="title">constructor</span>(<span class="params"></span>)</span> &#123; <span class="built_in">super</span>() &#125; &#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">new</span> A();</span><br><span class="line"><span class="keyword">new</span> B();</span><br></pre></td></tr></table></figure><ul><li>1: A, A</li><li>2: A, B</li></ul><details><summary><b>Answer</b></summary><p></p><h5 id="Answer-2-7"><a href="#Answer-2-7" class="headerlink" title="Answer: 2"></a>Answer: 2</h5><p>Using constructors, <code>new.target</code> refers to the constructor (points to the class definition of class which is initialized) that was directly invoked by new. This also applies to the case if the constructor is in a parent class and was delegated from a child constructor.</p><p></p></details><hr><h4 id="35-What-is-the-output-of-below-code"><a href="#35-What-is-the-output-of-below-code" class="headerlink" title="35. What is the output of below code"></a>35. What is the output of below code</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> [x, ...y,] = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>];</span><br><span class="line"><span class="built_in">console</span>.log(x, y);</span><br></pre></td></tr></table></figure><ul><li>1: 1, [2, 3, 4]</li><li>2: 1, [2, 3]</li><li>3: 1, [2]</li><li>4: SyntaxError</li></ul><details><summary><b>Answer</b></summary><p></p><h5 id="Answer-4-9"><a href="#Answer-4-9" class="headerlink" title="Answer: 4"></a>Answer: 4</h5><p>It throws a syntax error because the rest element should not have a trailing comma. You should always consider using a rest operator as the last element.</p><p></p></details><hr><h4 id="36-What-is-the-output-of-below-code"><a href="#36-What-is-the-output-of-below-code" class="headerlink" title="36. What is the output of below code"></a>36. What is the output of below code</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> &#123;<span class="attr">a</span>: x = <span class="number">10</span>, <span class="attr">b</span>: y = <span class="number">20</span>&#125; = &#123;<span class="attr">a</span>: <span class="number">30</span>&#125;;</span><br><span class="line"></span><br><span class="line"><span class="built_in">console</span>.log(x);</span><br><span class="line"><span class="built_in">console</span>.log(y);</span><br></pre></td></tr></table></figure><ul><li>1: 30, 20</li><li>2: 10, 20</li><li>3: 10, undefined</li><li>4: 30, undefined</li></ul><details><summary><b>Answer</b></summary><p></p><h5 id="Answer-1-10"><a href="#Answer-1-10" class="headerlink" title="Answer: 1"></a>Answer: 1</h5><p>The object property follows below rules,</p><ol><li>The object properties can be retrieved and assigned to a variable with a different name</li><li>The property assigned a default value when the retrieved value is <code>undefined</code></li></ol><p></p></details><hr><h4 id="37-What-is-the-output-of-below-code"><a href="#37-What-is-the-output-of-below-code" class="headerlink" title="37. What is the output of below code"></a>37. What is the output of below code</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">area</span>(<span class="params">&#123;length = <span class="number">10</span>, width = <span class="number">20</span>&#125;</span>) </span>&#123;</span><br><span class="line">  <span class="built_in">console</span>.log(length*width);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">area();</span><br></pre></td></tr></table></figure><ul><li>1: 200</li><li>2: Error</li><li>3: undefined</li><li>4: 0</li></ul><details><summary><b>Answer</b></summary><p></p><h5 id="Answer-2-8"><a href="#Answer-2-8" class="headerlink" title="Answer: 2"></a>Answer: 2</h5><p>If you leave out the right-hand side assignment for the destructuring object, the function will look for at least one argument to be supplied when invoked. Otherwise you will receive an error <code>Error: Cannot read property &#39;length&#39; of undefined</code> as mentioned above.</p><p>You can avoid the error with either of the below changes,</p><ol><li><strong>Pass at least an empty object:</strong></li></ol><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">area</span>(<span class="params">&#123;length = <span class="number">10</span>, width = <span class="number">20</span>&#125;</span>) </span>&#123;</span><br><span class="line">  <span class="built_in">console</span>.log(length*width);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">area(&#123;&#125;);</span><br></pre></td></tr></table></figure><ol start="2"><li><strong>Assign default empty object:</strong></li></ol><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">area</span>(<span class="params">&#123;length = <span class="number">10</span>, width = <span class="number">20</span>&#125; = &#123;&#125;</span>) </span>&#123;</span><br><span class="line">  <span class="built_in">console</span>.log(length*width);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">area();</span><br></pre></td></tr></table></figure><p></p></details><hr><h4 id="38-What-is-the-output-of-below-code"><a href="#38-What-is-the-output-of-below-code" class="headerlink" title="38. What is the output of below code"></a>38. What is the output of below code</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> props = [</span><br><span class="line">  &#123; <span class="attr">id</span>: <span class="number">1</span>, <span class="attr">name</span>: <span class="string">&#x27;John&#x27;</span>&#125;,</span><br><span class="line">  &#123; <span class="attr">id</span>: <span class="number">2</span>, <span class="attr">name</span>: <span class="string">&#x27;Jack&#x27;</span>&#125;,</span><br><span class="line">  &#123; <span class="attr">id</span>: <span class="number">3</span>, <span class="attr">name</span>: <span class="string">&#x27;Tom&#x27;</span>&#125;</span><br><span class="line">];</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> [,, &#123; name &#125;] = props;</span><br><span class="line"><span class="built_in">console</span>.log(name);</span><br></pre></td></tr></table></figure><ul><li>1: Tom</li><li>2: Error</li><li>3: undefined</li><li>4: John</li></ul><details><summary><b>Answer</b></summary><p></p><h5 id="Answer-1-11"><a href="#Answer-1-11" class="headerlink" title="Answer: 1"></a>Answer: 1</h5><p>It is possible to combine Array and Object destructuring. In this case, the third element in the array props accessed first followed by name property in the object.</p><p></p></details><hr><h4 id="39-What-is-the-output-of-below-code"><a href="#39-What-is-the-output-of-below-code" class="headerlink" title="39. What is the output of below code"></a>39. What is the output of below code</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">checkType</span>(<span class="params">num = <span class="number">1</span></span>) </span>&#123;</span><br><span class="line">  <span class="built_in">console</span>.log(<span class="keyword">typeof</span> num);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">checkType();</span><br><span class="line">checkType(<span class="literal">undefined</span>);</span><br><span class="line">checkType(<span class="string">&#x27;&#x27;</span>);</span><br><span class="line">checkType(<span class="literal">null</span>);</span><br></pre></td></tr></table></figure><ul><li>1: number, undefined, string, object</li><li>2: undefined, undefined, string, object</li><li>3: number, number, string, object</li><li>4: number, number, number, number</li></ul><details><summary><b>Answer</b></summary><p></p><h5 id="Answer-3-6"><a href="#Answer-3-6" class="headerlink" title="Answer: 3"></a>Answer: 3</h5><p>If the function argument is set implicitly(not passing argument) or explicitly to undefined, the value of the argument is the default parameter. Whereas for other falsy values(‘’ or null), the value of the argument is passed as a parameter.</p><p>Hence, the result of function calls categorized as below,</p><ol><li>The first two function calls logs number type since the type of default value is number</li><li>The type of ‘’ and null values are string and object type respectively.</li></ol><p></p></details><hr><h4 id="40-What-is-the-output-of-below-code"><a href="#40-What-is-the-output-of-below-code" class="headerlink" title="40. What is the output of below code"></a>40. What is the output of below code</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">add</span>(<span class="params">item, items = []</span>) </span>&#123;</span><br><span class="line">  items.push(item);</span><br><span class="line">  <span class="keyword">return</span> items;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="built_in">console</span>.log(add(<span class="string">&#x27;Orange&#x27;</span>));</span><br><span class="line"><span class="built_in">console</span>.log(add(<span class="string">&#x27;Apple&#x27;</span>));</span><br></pre></td></tr></table></figure><ul><li>1: [‘Orange’], [‘Orange’, ‘Apple’]</li><li>2: [‘Orange’], [‘Apple’]</li></ul><details><summary><b>Answer</b></summary><p></p><h5 id="Answer-2-9"><a href="#Answer-2-9" class="headerlink" title="Answer: 2"></a>Answer: 2</h5><p>Since the default argument is evaluated at call time, a new object is created each time the function is called. So in this case, the new array is created and an element pushed to the default empty array.</p><p></p></details><hr><h4 id="41-What-is-the-output-of-below-code"><a href="#41-What-is-the-output-of-below-code" class="headerlink" title="41. What is the output of below code"></a>41. What is the output of below code</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">greet</span>(<span class="params">greeting, name, message = greeting + <span class="string">&#x27; &#x27;</span> + name</span>) </span>&#123;</span><br><span class="line">  <span class="built_in">console</span>.log([name, greeting, message]);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">greet(<span class="string">&#x27;Hello&#x27;</span>, <span class="string">&#x27;John&#x27;</span>);</span><br><span class="line">greet(<span class="string">&#x27;Hello&#x27;</span>, <span class="string">&#x27;John&#x27;</span>, <span class="string">&#x27;Good morning!&#x27;</span>);</span><br></pre></td></tr></table></figure><ul><li>1: SyntaxError</li><li>2: [‘Hello’, ‘John’, ‘Hello John’], [‘Hello’, ‘John’, ‘Good morning!’]</li></ul><details><summary><b>Answer</b></summary><p></p><h5 id="Answer-2-10"><a href="#Answer-2-10" class="headerlink" title="Answer: 2"></a>Answer: 2</h5><p>Since parameters defined earlier are available to later default parameters, this code snippet doesn’t throw any error.</p><p></p></details><hr><h4 id="42-What-is-the-output-of-below-code"><a href="#42-What-is-the-output-of-below-code" class="headerlink" title="42. What is the output of below code"></a>42. What is the output of below code</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">outer</span>(<span class="params">f = inner()</span>) </span>&#123;</span><br><span class="line">  <span class="function"><span class="keyword">function</span> <span class="title">inner</span>(<span class="params"></span>) </span>&#123; <span class="keyword">return</span> <span class="string">&#x27;Inner&#x27;</span> &#125;</span><br><span class="line">&#125;</span><br><span class="line">outer();</span><br></pre></td></tr></table></figure><ul><li>1: ReferenceError</li><li>2: Inner</li></ul><details><summary><b>Answer</b></summary><p></p><h5 id="Answer-1-12"><a href="#Answer-1-12" class="headerlink" title="Answer: 1"></a>Answer: 1</h5><p>The functions and variables declared in the function body cannot be referred from default value parameter initializers. If you still try to access, it throws a run-time ReferenceError(i.e, <code>inner</code> is not defined).</p><p></p></details><hr><h4 id="43-What-is-the-output-of-below-code"><a href="#43-What-is-the-output-of-below-code" class="headerlink" title="43. What is the output of below code"></a>43. What is the output of below code</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">myFun</span>(<span class="params">x, y, ...manyMoreArgs</span>) </span>&#123;</span><br><span class="line">  <span class="built_in">console</span>.log(manyMoreArgs)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">myFun(<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>);</span><br><span class="line">myFun(<span class="number">1</span>, <span class="number">2</span>);</span><br></pre></td></tr></table></figure><ul><li>1: [3, 4, 5], undefined</li><li>2: SyntaxError</li><li>3: [3, 4, 5], []</li><li>4: [3, 4, 5], [undefined]</li></ul><details><summary><b>Answer</b></summary><p></p><h5 id="Answer-3-7"><a href="#Answer-3-7" class="headerlink" title="Answer: 3"></a>Answer: 3</h5><p>The rest parameter is used to hold the remaining parameters of a function and it becomes an empty array if the argument is not provided.</p><p></p></details><hr><h4 id="44-What-is-the-output-of-below-code"><a href="#44-What-is-the-output-of-below-code" class="headerlink" title="44. What is the output of below code"></a>44. What is the output of below code</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> obj = &#123;<span class="string">&#x27;key&#x27;</span>: <span class="string">&#x27;value&#x27;</span>&#125;;</span><br><span class="line"><span class="keyword">const</span> array = [...obj];</span><br><span class="line"><span class="built_in">console</span>.log(array);</span><br></pre></td></tr></table></figure><ul><li>1: [‘key’, ‘value’]</li><li>2: TypeError</li><li>3: []</li><li>4: [‘key’]</li></ul><details><summary><b>Answer</b></summary><p></p><h5 id="Answer-2-11"><a href="#Answer-2-11" class="headerlink" title="Answer: 2"></a>Answer: 2</h5><p>Spread syntax can be applied only to iterable objects. By default, Objects are not iterable, but they become iterable when used in an Array, or with iterating functions such as <code>map(), reduce(), and assign()</code>. If you still try to do it, it still throws <code>TypeError: obj is not iterable</code>.</p><p></p></details><hr><h4 id="45-What-is-the-output-of-below-code"><a href="#45-What-is-the-output-of-below-code" class="headerlink" title="45. What is the output of below code"></a>45. What is the output of below code</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span>* <span class="title">myGenFunc</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="keyword">yield</span> <span class="number">1</span>;</span><br><span class="line">    <span class="keyword">yield</span> <span class="number">2</span>;</span><br><span class="line">    <span class="keyword">yield</span> <span class="number">3</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">var</span> myGenObj = <span class="keyword">new</span> myGenFunc;</span><br><span class="line"><span class="built_in">console</span>.log(myGenObj.next().value);</span><br></pre></td></tr></table></figure><ul><li>1: 1</li><li>2: undefined</li><li>3: SyntaxError</li><li>4: TypeError</li></ul><details><summary><b>Answer</b></summary><p></p><h5 id="Answer-4-10"><a href="#Answer-4-10" class="headerlink" title="Answer: 4"></a>Answer: 4</h5><p>Generators are not constructible type. But if you still proceed to do, there will be an error saying “TypeError: myGenFunc is not a constructor”</p><p></p></details><hr><h4 id="46-What-is-the-output-of-below-code"><a href="#46-What-is-the-output-of-below-code" class="headerlink" title="46. What is the output of below code"></a>46. What is the output of below code</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span>* <span class="title">yieldAndReturn</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">  <span class="keyword">yield</span> <span class="number">1</span>;</span><br><span class="line">  <span class="keyword">return</span> <span class="number">2</span>;</span><br><span class="line">  <span class="keyword">yield</span> <span class="number">3</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> myGenObj = yieldAndReturn()</span><br><span class="line"><span class="built_in">console</span>.log(myGenObj.next());</span><br><span class="line"><span class="built_in">console</span>.log(myGenObj.next());</span><br><span class="line"><span class="built_in">console</span>.log(myGenObj.next());</span><br></pre></td></tr></table></figure><ul><li>1: { value: 1, done: false }, { value: 2, done: true }, { value: undefined, done: true }</li><li>2: { value: 1, done: false }, { value: 2, done: false }, { value: undefined, done: true }</li><li>3: { value: 1, done: false }, { value: 2, done: true }, { value: 3, done: true }</li><li>4: { value: 1, done: false }, { value: 2, done: false }, { value: 3, done: true }</li></ul><details><summary><b>Answer</b></summary><p></p><h5 id="Answer-1-13"><a href="#Answer-1-13" class="headerlink" title="Answer: 1"></a>Answer: 1</h5><p>A return statement in a generator function will make the generator finish. If a value is returned, it will be set as the value property of the object and done property to true. When a generator is finished, subsequent next() calls return an object of this form: <code>&#123;value: undefined, done: true&#125;</code>.</p><p></p></details><hr><h4 id="47-What-is-the-output-of-below-code"><a href="#47-What-is-the-output-of-below-code" class="headerlink" title="47. What is the output of below code"></a>47. What is the output of below code</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> myGenerator = (<span class="function"><span class="keyword">function</span> *(<span class="params"></span>)</span>&#123;</span><br><span class="line">  <span class="keyword">yield</span> <span class="number">1</span>;</span><br><span class="line">  <span class="keyword">yield</span> <span class="number">2</span>;</span><br><span class="line">  <span class="keyword">yield</span> <span class="number">3</span>;</span><br><span class="line">&#125;)();</span><br><span class="line"><span class="keyword">for</span> (<span class="keyword">const</span> value <span class="keyword">of</span> myGenerator) &#123;</span><br><span class="line">  <span class="built_in">console</span>.log(value);</span><br><span class="line">  <span class="keyword">break</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">for</span> (<span class="keyword">const</span> value <span class="keyword">of</span> myGenerator) &#123;</span><br><span class="line">  <span class="built_in">console</span>.log(value);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><ul><li>1: 1,2,3 and 1,2,3</li><li>2: 1,2,3 and 4,5,6</li><li>3: 1 and 1</li><li>4: 1</li></ul><details><summary><b>Answer</b></summary><p></p><h5 id="Answer-4-11"><a href="#Answer-4-11" class="headerlink" title="Answer: 4"></a>Answer: 4</h5><p>The generator should not be re-used once the iterator is closed. i.e, Upon exiting a loop(on completion or using break &amp; return), the generator is closed and trying to iterate over it again does not yield any more results. Hence, the second loop doesn’t print any value.</p><p></p></details><hr><h4 id="48-What-is-the-output-of-below-code"><a href="#48-What-is-the-output-of-below-code" class="headerlink" title="48. What is the output of below code"></a>48. What is the output of below code</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> num = 0o38;</span><br><span class="line"><span class="built_in">console</span>.log(num);</span><br></pre></td></tr></table></figure><ul><li>1: SyntaxError</li><li>2: 38</li></ul><details><summary><b>Answer</b></summary><p></p><h5 id="Answer-1-14"><a href="#Answer-1-14" class="headerlink" title="Answer: 1"></a>Answer: 1</h5><p>If you use an invalid number(outside of 0-7 range) in the octal literal, JavaScript will throw a SyntaxError. In ES5, it treats the octal literal as a decimal number.</p><p></p></details><hr><h4 id="49-What-is-the-output-of-below-code"><a href="#49-What-is-the-output-of-below-code" class="headerlink" title="49. What is the output of below code"></a>49. What is the output of below code</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> squareObj = <span class="keyword">new</span> Square(<span class="number">10</span>);</span><br><span class="line"><span class="built_in">console</span>.log(squareObj.area);</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Square</span> </span>&#123;</span><br><span class="line">  <span class="function"><span class="title">constructor</span>(<span class="params">length</span>)</span> &#123;</span><br><span class="line">    <span class="built_in">this</span>.length = length;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">get</span> <span class="title">area</span>() &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="built_in">this</span>.length * <span class="built_in">this</span>.length;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">set</span> <span class="title">area</span>(<span class="params">value</span>) &#123;</span><br><span class="line">    <span class="built_in">this</span>.area = value;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><ul><li>1: 100</li><li>2: ReferenceError</li></ul><details><summary><b>Answer</b></summary><p></p><h5 id="Answer-2-12"><a href="#Answer-2-12" class="headerlink" title="Answer: 2"></a>Answer: 2</h5><p>Unlike function declarations, class declarations are not hoisted. i.e, First You need to declare your class and then access it, otherwise it will throw a ReferenceError “Uncaught ReferenceError: Square is not defined”.</p><p><strong>Note:</strong> Class expressions also applies to the same hoisting restrictions of class declarations.</p><p></p></details><hr><h4 id="50-What-is-the-output-of-below-code"><a href="#50-What-is-the-output-of-below-code" class="headerlink" title="50. What is the output of below code"></a>50. What is the output of below code</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">Person</span>(<span class="params"></span>) </span>&#123; &#125;</span><br><span class="line"></span><br><span class="line">Person.prototype.walk = <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">  <span class="keyword">return</span> <span class="built_in">this</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">Person.run = <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">  <span class="keyword">return</span> <span class="built_in">this</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> user = <span class="keyword">new</span> Person();</span><br><span class="line"><span class="keyword">let</span> walk = user.walk;</span><br><span class="line"><span class="built_in">console</span>.log(walk());</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> run = Person.run;</span><br><span class="line"><span class="built_in">console</span>.log(run());</span><br></pre></td></tr></table></figure><ul><li>1: undefined, undefined</li><li>2: Person, Person</li><li>3: SyntaxError</li><li>4: Window, Window</li></ul><details><summary><b>Answer</b></summary><p></p><h5 id="Answer-4-12"><a href="#Answer-4-12" class="headerlink" title="Answer: 4"></a>Answer: 4</h5><p>When a regular or prototype method is called without a value for <strong>this</strong>, the methods return an initial this value if the value is not undefined. Otherwise global window object will be returned. In our case, the initial <code>this</code> value is undefined so both methods return window objects.</p><p></p></details><hr><h4 id="51-What-is-the-output-of-below-code"><a href="#51-What-is-the-output-of-below-code" class="headerlink" title="51. What is the output of below code"></a>51. What is the output of below code</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Vehicle</span> </span>&#123;</span><br><span class="line">  <span class="function"><span class="title">constructor</span>(<span class="params">name</span>)</span> &#123;</span><br><span class="line">    <span class="built_in">this</span>.name = name;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="function"><span class="title">start</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">`<span class="subst">$&#123;<span class="built_in">this</span>.name&#125;</span> vehicle started`</span>);</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Car</span> <span class="keyword">extends</span> <span class="title">Vehicle</span> </span>&#123;</span><br><span class="line">  <span class="function"><span class="title">start</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">`<span class="subst">$&#123;<span class="built_in">this</span>.name&#125;</span> car started`</span>);</span><br><span class="line">    <span class="built_in">super</span>.start();</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> car = <span class="keyword">new</span> Car(<span class="string">&#x27;BMW&#x27;</span>);</span><br><span class="line"><span class="built_in">console</span>.log(car.start());</span><br></pre></td></tr></table></figure><ul><li>1: SyntaxError</li><li>2: BMW vehicle started, BMW car started</li><li>3: BMW car started, BMW vehicle started</li><li>4: BMW car started, BMW car started</li></ul><details><summary><b>Answer</b></summary><p></p><h5 id="Answer-3-8"><a href="#Answer-3-8" class="headerlink" title="Answer: 3"></a>Answer: 3</h5><p>The super keyword is used to call methods of a superclass. Unlike other languages the super invocation doesn’t need to be a first statement. i.e, The statements will be executed in the same order of code.</p><p></p></details><hr><h4 id="52-What-is-the-output-of-below-code"><a href="#52-What-is-the-output-of-below-code" class="headerlink" title="52. What is the output of below code"></a>52. What is the output of below code</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> USER = &#123;<span class="string">&#x27;age&#x27;</span>: <span class="number">30</span>&#125;;</span><br><span class="line">USER.age = <span class="number">25</span>;</span><br><span class="line"><span class="built_in">console</span>.log(USER.age);</span><br></pre></td></tr></table></figure><ul><li>1: 30</li><li>2: 25</li><li>3: Uncaught TypeError</li><li>4: SyntaxError</li></ul><details><summary><b>Answer</b></summary><p></p><h5 id="Answer-2-13"><a href="#Answer-2-13" class="headerlink" title="Answer: 2"></a>Answer: 2</h5><p>Even though we used constant variables, the content of it is an object and the object’s contents (e.g properties) can be altered. Hence, the change is going to be valid in this case.</p><p></p></details><hr><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p><h4 id="53-What-is-the-output-of-below-code"><a href="#53-What-is-the-output-of-below-code" class="headerlink" title="53. What is the output of below code"></a>53. What is the output of below code</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">console</span>.log(<span class="string">&#x27;🙂&#x27;</span> === <span class="string">&#x27;🙂&#x27;</span>);</span><br></pre></td></tr></table></figure><ul><li>1: false</li><li>2: true</li></ul><details><summary><b>Answer</b></summary><p></p><h5 id="Answer-2-14"><a href="#Answer-2-14" class="headerlink" title="Answer: 2"></a>Answer: 2</h5><p>Emojis are unicodes and the unicode for smile symbol is “U+1F642”. The unicode comparision of same emojies is equivalent to string comparison. Hence, the output is always true.</p><p></p></details><hr><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p><h4 id="54-What-is-the-output-of-below-code"><a href="#54-What-is-the-output-of-below-code" class="headerlink" title="54. What is the output of below code?"></a>54. What is the output of below code?</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">console</span>.log(<span class="keyword">typeof</span> <span class="keyword">typeof</span> <span class="keyword">typeof</span> <span class="literal">true</span>);</span><br></pre></td></tr></table></figure><ul><li>1: string</li><li>2: boolean</li><li>3: NaN</li><li>4: number</li></ul><details><summary><b>Answer</b></summary><p></p><h5 id="Answer-1-15"><a href="#Answer-1-15" class="headerlink" title="Answer: 1"></a>Answer: 1</h5><p>The typeof operator on any primitive returns a string value. So even if you apply the chain of typeof operators on the return value, it is always string.</p><p></p></details><hr><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p><h4 id="55-What-is-the-output-of-below-code"><a href="#55-What-is-the-output-of-below-code" class="headerlink" title="55. What is the output of below code?"></a>55. What is the output of below code?</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> zero = <span class="keyword">new</span> <span class="built_in">Number</span>(<span class="number">0</span>);</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> (zero) &#123;</span><br><span class="line">  <span class="built_in">console</span>.log(<span class="string">&quot;If&quot;</span>);</span><br><span class="line">&#125; <span class="keyword">else</span> &#123;</span><br><span class="line">  <span class="built_in">console</span>.log(<span class="string">&quot;Else&quot;</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><ul><li>1: If</li><li>2: Else</li><li>3: NaN</li><li>4: SyntaxError</li></ul><details><summary><b>Answer</b></summary><p></p><h5 id="Answer-1-16"><a href="#Answer-1-16" class="headerlink" title="Answer: 1"></a>Answer: 1</h5><ol><li>The type of operator on new Number always returns object. i.e, typeof new Number(0) –&gt; object.</li><li>Objects are always truthy in if block</li></ol><p>Hence the above code block always goes to if section.</p><p></p></details><hr><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p><h4 id="55-What-is-the-output-of-below-code-in-non-strict-mode"><a href="#55-What-is-the-output-of-below-code-in-non-strict-mode" class="headerlink" title="55. What is the output of below code in non strict mode?"></a>55. What is the output of below code in non strict mode?</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> msg = <span class="string">&quot;Good morning!!&quot;</span>;</span><br><span class="line"></span><br><span class="line">msg.name = <span class="string">&quot;John&quot;</span>; </span><br><span class="line"></span><br><span class="line"><span class="built_in">console</span>.log(msg.name);</span><br></pre></td></tr></table></figure><ul><li>1: “”</li><li>2: Error</li><li>3: John</li><li>4: Undefined</li></ul><details><summary><b>Answer</b></summary><p></p><h5 id="Answer-4-13"><a href="#Answer-4-13" class="headerlink" title="Answer: 4"></a>Answer: 4</h5><p>It returns undefined for non-strict mode and returns Error for strict mode. In non-strict mode, the wrapper object is going to be created and get the mentioned property. But the object get disappeared after accessing the property in next line.</p><p></p></details><hr><p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p></div><footer><p class="meta"><span class="byline author vcard">Posted by <span class="fn">BARAN</span></span> <time datetime="2020-12-02T16:00:00.000Z" data-updated="true">Thu Dec 03 2020</time> <span class="categories"><a class="category" href="/tags/js/">js</a>, <a class="category" href="/tags/problem/">problem</a></span></p><div class="sharing"><a class="twitter-share-button" target="_blank" rel="noopener" href="https://twitter.com/intent/tweet?text=js%20%E4%B8%AD%E5%B8%B8%E8%A7%81%E9%97%AE%E9%A2%98%E6%95%B4%E7%90%86%20400%2B">Tweet</a></div><p class="meta"><a class="basic-alignment left" href="/2020/12/04/web/js/plugs/h5_click_delay/" title="Previous Post: 移动端 js 优化 css active 方法">« 移动端 js 优化 css active 方法</a> <a class="basic-alignment right" href="/2020/11/30/nodejs/pm2/" title="Previous Post: linux 后台运行 node 方法 😋">linux 后台运行 node 方法 😋 »</a></p></footer></article></div><aside class="sidebar"><section class="odd"><h1>最近的文章</h1><ul class="post-list"><li class="post-list-item"><a class="post-list-link" href="/2021/01/29/nodejs/webpack/">webpack</a></li><li class="post-list-item"><a class="post-list-link" href="/2021/01/27/database/redis/">redis 😋</a></li><li class="post-list-item"><a class="post-list-link" href="/2021/01/26/nodejs/axios/">axios 使用 class 封装下</a></li><li class="post-list-item"><a class="post-list-link" href="/2021/01/24/web/js/frame_lib/">框架 & 库 😋</a></li><li class="post-list-item"><a class="post-list-link" href="/2021/01/09/nodejs/lerna/">lerna 源码阅读</a></li><li class="post-list-item"><a class="post-list-link" href="/2021/01/08/npm/base/">npm 😋</a></li><li class="post-list-item"><a class="post-list-link" href="/2020/12/31/nodejs/cli/">脚手架 😋</a></li><li class="post-list-item"><a class="post-list-link" href="/2020/12/29/hexo/music/">hexo 使用 QQ音乐 😋</a></li><li class="post-list-item"><a class="post-list-link" href="/2020/12/25/system/jumpserver/">JumpServer 😋</a></li><li class="post-list-item"><a class="post-list-link" href="/2020/12/19/web/js/algorithm/">js 算法杂记</a></li></ul></section><section class="even"><h1>精选文章</h1><ul><li class="post"><a href="/2021/01/29/nodejs/webpack/">webpack</a></li><li class="post"><a href="/2020/12/31/nodejs/cli/">脚手架 😋</a></li><li class="post"><a href="/2020/12/17/nodejs/rollup/">从配置文件认识 rollup</a></li><li class="post"><a href="/2020/11/07/tool/soft/vscode/">vscode 😋</a></li></ul></section><section class="odd"><h1>GitHub 仓库</h1><ul id="gh_repos"><li class="loading">Status updating&#8230;</li></ul><a target="_blank" rel="noopener" href="https://github.com/cxvh">@cxvh</a> on GitHub<script>$(document).ready(function(){var r="#gh_repos";"localhost"!==location.hostname&&$.ajax({url:"https://api.github.com/users/cxvh/repos?sort=pushed&callback=?",dataType:"jsonp",error:function(a){$(r+" li.loading").addClass("error").text("Error loading feed")},success:function(a){var r=[];if(a&&a.data){for(var t=0;t<a.data.length;t++)a.data[t].fork||r.push(a.data[t]);!function(a){let r,t="",o=$("#gh_repos")[0];for(r=0;r<a.length;r++)t+='<li><a href="'+a[r].html_url+'">'+a[r].name+"</a><p>"+(e=a[r].description||"",$("<div/>").text(e).html()+"</p></li>");var e;o.innerHTML=t}(r)}}})})</script></section><section class="odd"><h1>友情链接</h1><ul id="links" class="links"><li class="link"><img src="https://blog-1303885568.cos.ap-chengdu.myqcloud.com/useImg/avat.jpg"> <a target="_blank" rel="noopener" href="https://www.dingshiyi.top&#34;" title="Loneliness">Loneliness</a><p title="万水千山，你愿意陪我一起看吗">万水千山，你愿意陪我一起看吗</p></li><li class="link"><img src="https://image.theeye.tech/img/15873849679911587384967991mmexport1569296865219---xinsiraa.png"> <a target="_blank" rel="noopener" href="https://theeye.tech/" title="慧眼博客">慧眼博客</a><p title="xinsir的个人博客">xinsir的个人博客</p></li><li class="link"><img src="https://miluluyo.github.io/vdoingBlog/img/tx.jpg"> <a target="_blank" rel="noopener" href="https://www.cnblogs.com/miluluyo/" title="麋鹿鲁哟">麋鹿鲁哟</a><p title="大道至简，知易行难。">大道至简，知易行难。</p></li><li class="applylink"><a target="_blank" rel="noopener" href="http://wpa.qq.com/msgrd?v=3&uin=630749264&site=qq&menu=yes">申请友链🤝🤝🤝</a></li></ul></section></aside></div><div id="fishContainer"></div><span class="dot-one"></span> <span class="dot-two"></span> <span class="dot-three"></span></div><div class="lantern-one"><div class="lantern"><div class="line"></div><div class="lantern-a"><div class="lantern-b"><div class="lantern-t">新春</div></div></div><div class="water water-a"><div class="water-c"></div><div class="water-b"></div></div></div></div><div class="lantern-two"><div class="lantern"><div class="line"></div><div class="lantern-a"><div class="lantern-b"><div class="lantern-t">快乐</div></div></div><div class="water water-a"><div class="water-c"></div><div class="water-b"></div></div></div></div><footer role="contentinfo"><p>Copyright © 2020 - 2021 BARAN - <span class="credit"><a target="_blank" rel="noopener" href="http://beian.miit.gov.cn">陕ICP备2020016132号-1</a>&nbsp; <a target="_blank" href="http://www.beian.gov.cn/portal/registerSystemInfo?recordcode=61011302001121"><img src="/images/icon/beian.gov.png"><span style="padding-left:5px">陕公网安备 61011302001121号</span></a>&nbsp; UV: <span id="busuanzi_value_site_uv"></span>&nbsp; PV: <span id="busuanzi_value_site_pv"></span>&nbsp; IP:<span id="userIp">loading...</span>&nbsp; 💖已运行:<span id="runtime"></span></span></p></footer><script>!function(f){function e(e){for(var r,t,n=e[0],o=e[1],u=e[2],i=0,l=[];i<n.length;i++)t=n[i],Object.prototype.hasOwnProperty.call(p,t)&&p[t]&&l.push(p[t][0]),p[t]=0;for(r in o)Object.prototype.hasOwnProperty.call(o,r)&&(f[r]=o[r]);for(s&&s(e);l.length;)l.shift()();return c.push.apply(c,u||[]),a()}function a(){for(var e,r=0;r<c.length;r++){for(var t=c[r],n=!0,o=1;o<t.length;o++){var u=t[o];0!==p[u]&&(n=!1)}n&&(c.splice(r--,1),e=i(i.s=t[0]))}return e}var t={},p={1:0},c=[];function i(e){if(t[e])return t[e].exports;var r=t[e]={i:e,l:!1,exports:{}};return f[e].call(r.exports,r,r.exports,i),r.l=!0,r.exports}i.m=f,i.c=t,i.d=function(e,r,t){i.o(e,r)||Object.defineProperty(e,r,{enumerable:!0,get:t})},i.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},i.t=function(r,e){if(1&e&&(r=i(r)),8&e)return r;if(4&e&&"object"==typeof r&&r&&r.__esModule)return r;var t=Object.create(null);if(i.r(t),Object.defineProperty(t,"default",{enumerable:!0,value:r}),2&e&&"string"!=typeof r)for(var n in r)i.d(t,n,function(e){return r[e]}.bind(null,n));return t},i.n=function(e){var r=e&&e.__esModule?function(){return e.default}:function(){return e};return i.d(r,"a",r),r},i.o=function(e,r){return Object.prototype.hasOwnProperty.call(e,r)},i.p="/";var r=this.webpackJsonpgrid=this.webpackJsonpgrid||[],n=r.push.bind(r);r.push=e,r=r.slice();for(var o=0;o<r.length;o++)e(r[o]);var s=n;a()}([])</script><script src="https://cdn.cxvh.com/script/js/fish.min.js"></script><script src="/live2dw/lib/L2Dwidget.min.js"></script><link rel="stylesheet" href="https://cdn.cxvh.com/script/viewer/viewer.min.css"><script src="https://cdn.cxvh.com/script/viewer/viewer.min.js"></script><script src="https://cdn.cxvh.com/script/jquery-viewer/jquery-viewer.min.js"></script><script src="//y.gtimg.cn/music/h5/player/player.js?max_age=2592000"></script><div class="music-player" style="display:none" id="musicPlayer"><div class="circle"><div class="img" id="circleImg"></div></div><div class="btn-box"><div class="btn play" music-play><svg class="play" viewBox="0 0 64 64"><path d="M51.109 30.335l-36-24A2 2 0 0 0 12 8v48a2.003 2.003 0 0 0 2 2c.388 0 .775-.113 1.109-.336l36-24a2 2 0 0 0 0-3.329z"/></svg> <svg class="pause" viewBox="0 0 100 100"><path d="M22.537 8.046h17.791c1.104 0 2.003.898 2.003 1.993v79.912a2.005 2.005 0 0 1-2.003 2.003h-17.79a2.005 2.005 0 0 1-2.003-2.003V10.04c0-1.095.898-1.993 2.002-1.993zM59.672 8.046h17.8c1.095 0 1.993.898 1.993 1.993v79.912a2.003 2.003 0 0 1-1.993 2.003h-17.8a1.997 1.997 0 0 1-1.993-2.003V10.04c0-1.095.889-1.993 1.993-1.993z"/></svg></div><div class="btn next" music-next><svg viewBox="0 0 100 100"><path d="M26.695 34.434v31.132L54.5 49.998z"/><path d="M24.07 34.434v31.132c0 2.018 2.222 3.234 3.95 2.267l27.804-15.568c1.706-.955 1.707-3.578 0-4.533L28.02 32.168c-2.957-1.655-5.604 2.88-2.649 4.533l27.805 15.564v-4.533L25.371 63.3l3.95 2.267V34.435c-.001-3.387-5.251-3.387-5.251-.001z"/><g><path d="M55.5 34.434v31.132l27.805-15.568z"/><path d="M52.875 34.434v31.132c0 2.018 2.222 3.234 3.949 2.267 9.27-5.189 18.537-10.379 27.805-15.568 1.705-.955 1.705-3.578 0-4.533L56.824 32.168c-2.957-1.655-5.604 2.88-2.648 4.533l27.803 15.564v-4.533L54.176 63.3l3.949 2.267V34.435c0-3.387-5.25-3.387-5.25-.001z"/></g></svg></div></div></div><script src="/js/octo.js"></script><script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></body></html>